【发布时间】:2017-03-04 05:21:32
【问题描述】:
我有一个使用 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存在于 sb 文件夹中?如果它们都在同一个文件夹中,那么试试这个./sb.component.html -
是的,它存在于 sb 文件夹中,app.ts 存在于该文件夹之外。
-
在
SBComponent里面把templateUrl改成templateUrl: './sb.component.html' -
谢谢你!那行得通!我投了一票,这算作回答吗?
-
@eagleEye 很高兴它有帮助:)
标签: angular angular2-template angular2-modules