我有一个使用Angular-CLI创建的简单应用程序,我想重构并将app.component.ts代码移动到单独的组件文件中,但开始出现错误:
找不到模块:错误:无法解析对'./sb/sb.component.html'(我的SBComponent)的引用。
这是我目前的代码:
app.module:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { AppComponent } from './app.component';
import { SBComponent } from './sb/sb.component'
@NgModule({
declarations: [
AppComponent, SBComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
app.component:
import { Component } from '@angular/core';
import { NgModule,ElementRef, ViewChild, AfterViewInit} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
}
新组件:
import { Component, OnInit } from '@angular/core';
import { NgModule,ElementRef, ViewChild, AfterViewInit, AfterContentInit} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'
@Component({
selector: 'app-sb',
templateUrl: './sb/sb.component.html'
})
export class SBComponent {
}
非常感谢任何帮助!
sb.component.html
exist inside sb folder ? if they are both in the same folder then try this./sb.component.html
- Idrees KhanSBComponent
中将templateUrl
改为templateUrl: './sb.component.html'
。 - Idrees Khan