【发布时间】:2020-01-14 22:12:35
【问题描述】:
我刚刚开始学习 Angular 并完成了一些教程。我的项目是由 Angular CLI 生成的。我在创建项目时生成的组件之上生成了一个名为 navbar 的新组件,我试图查看启动时导航栏是否加载到我的 index.html 上。仅当我在 index.html 文件中有两个应用程序时,我的导航栏才会显示,例如:
<body>
<app-root></app-root>
<app-navbar></app-navbar>
</body>
如果我像这样从 index.html 中删除 app-root:
<body>
<app-navbar></app-navbar>
</body>
我的导航栏应用不再显示它。这与 app-root 组件有关吗?是不是因为它是根组件,必须一直包含在 index.html 中?
这是我的代码:
index.html:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
<app-navbar></app-navbar>
</body>
</html>
app.module.ts:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './component1/app.component';
import { NavbarComponent } from './navbar/navbar.component';
@NgModule({
declarations: [
AppComponent,
NavbarComponent
],
imports: [
BrowserModule,
AppRoutingModule
],
providers: [],
bootstrap: [
AppComponent,
NavbarComponent
]
})
export class AppModule { }
navbar.component.ts:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-navbar',
templateUrl: './navbar.component.html',
styleUrls: ['./navbar.component.css']
})
export class NavbarComponent {
constructor() {}
// tslint:disable-next-line: use-lifecycle-interface
ngOnInit() {
}
}
【问题讨论】:
标签: angular typescript bootstrap-4 angular-components