【问题标题】:Loading second component causes "The template specified for component SidebarComponent is not a string"加载第二个组件导致“为组件 SidebarComponent 指定的模板不是字符串”
【发布时间】:2017-11-18 14:59:45
【问题描述】:

我刚刚开始学习 Angular,并尝试创建一个简单的仪表板。

我创建了 2 个组件,DashboardComponent 和 SidebarComponent。

仪表板加载正常,但是当我加载 SidebarComponent 时,浏览器出现错误“为组件 SidebarComponent 指定的模板不是字符串”

侧边栏组件:

import { Component } from '@angular/core';

@Component({
    selector: 'sidebar-component',
    templateUrl: './sidebar.component.ts',
    styleUrls: ['./sidebar.component.scss']
})
export class SidebarComponent {}

App.module

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';
import { DashboardComponent } from './dashboard/dashboard.component';
import { SidebarComponent } from './sidebar/sidebar.component';

@NgModule({
  declarations: [
    AppComponent,
    DashboardComponent,
    SidebarComponent
  ],
  imports: [
    BrowserModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

这两个也是在app.component中加载的

<sidebar-component></sidebar-component>
<dashboard></dashboard>

【问题讨论】:

  • 错误不言自明...您指的是.ts文件而不是.html...templateUrl: './sidebar.component.ts',
  • 啊!非常感谢您,请将此作为答案发布,以便我接受它
  • 但即使我添加html 而不是ts,我的第二个文件的component.ts 也不会绑定数据/被调用。

标签: angular typescript components


【解决方案1】:

错误不言自明……

您指的是.ts 文件而不是.html

改变这一行:

templateUrl: './sidebar.component.ts'

到:

templateUrl: './sidebar.component.html'

【讨论】:

    猜你喜欢
    • 2019-09-02
    • 1970-01-01
    • 2018-07-05
    • 2021-08-07
    • 2017-09-06
    • 2011-01-11
    • 1970-01-01
    • 2019-11-27
    • 1970-01-01
    相关资源
    最近更新 更多