当我为站点提供服务并检查页面时,我可以在Chrome开发人员工具中看到选择器,但模板中的html没有出现。组件是直接从angular cli添加的,我所做的就是将开始/结束选择器标记添加到index.html文件中。控制台中没有错误。
这是我在Chrome DOM中看到的:
<body>
<app-header></app-header>
<app-root></app-root>
<app-footer></app-footer>footer.component.ts:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-footer',
templateUrl: './footer.component.html',
styleUrls: ['./footer.component.css']
})
export class FooterComponent implements OnInit {
constructor() { }
ngOnInit() {
}
}footer.component.html
<p>
footer works!
</p>app.module.ts:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { HeaderComponent } from './header/header.component';
import { FooterComponent } from './footer/footer.component';
@NgModule({
declarations: [
AppComponent,
HeaderComponent,
FooterComponent
],
imports: [
BrowserModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }运行: npm版本5.5.1 Angular 5.1.0 angular cli 1.6.0
在Visual Studio代码中从命令行使用ng-serve。
发布于 2017-12-15 14:03:41
哦,你应该添加你的
<app-header></app-header>
and
<app-footer></app-footer>在app.component.html中不在index.html中
发布于 2017-12-15 12:23:36
我很确定你的AppModule的bootstrap数组里面有[AppComponent]。这就是一个Angular应用的启动方式。你引导一个模块的AppModule,在这个模块的bootstrap数组中有AppComponent。所以无论在<app-root></app-root>中是什么,你的Angular应用都会被渲染。
您可能希望将<app-header></app-header>和<app-footer></app-footer>放入您的App.component.html中
https://stackoverflow.com/questions/47825571
复制相似问题