angular - 为什么我的标题没有在这个简单的 Angular 应用程序中呈现?
问题描述
我是 Angular 的新手,我正在创建一个简单的应用程序来动手学习。我开始使用shared.module.ts
我的标题并将其导入我app.module.ts
的,但是当我运行应用程序时,我看不到我的<app-header></app-header>
渲染。下面是我的代码,有人能告诉我什么是错的吗?我相信我正在以正确的方式导出/导入/声明事物。
shared.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { HeaderComponent } from './components/header/header.component';
@NgModule({
declarations: [HeaderComponent],
imports: [
CommonModule
],
exports: [
HeaderComponent // exporting header component from shared module here
]
})
export class SharedModule { }
app.module.ts
//predefined modules
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
//application modules
import { AppRoutingModule } from './app-routing.module';
import {BlogModule} from './blog/blog.module';
import { SharedModule} from './shared/shared.module'
//components
import { AppComponent } from './app.component';
import { DashboardComponent } from './dashboard/dashboard.component';
@NgModule({
declarations: [
AppComponent,
DashboardComponent//declaring the dashboard component
],
imports: [
BrowserModule,
AppRoutingModule,
BlogModule,
SharedModule //importing the shared module here
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
仪表板.component.html
Hello, Header is below
<app-header></app-header>
我在浏览器上看到的:
您可以看到标头没有加载,并且控制台中也没有错误。
解决方案
您已经在 App 模块中声明了 Dashboard 组件,并在此处导入 Shared 模块。但是您在 Dashboard 组件中使用 Header 组件,而不是 App 组件。它行不通。您应该为 Dashboard 组件使用单独的模块并在那里导入 Shared 模块以在其中使用 Header 组件。或者只是在 app.component.html 中使用 Header 组件。
推荐阅读
- python - 使用 Python 运行脚本后关闭 CMD
- css - 使用滚动行为:仅使用 CSS 的一页平滑
- excel-formula - Excel:如何将每行 Y 列中的值(“所有匹配”)与 X 列中的匹配值进行比较?
- swiper - Swiper Js - 无法从图像帖子中删除覆盖线性背景
- assembly - 更改后要打印内存中的原始值
- javascript - 我有一个 HTML 表单,它使用 POST 请求和 PHP 文件自动向我发送电子邮件,但它不起作用……有什么建议吗?
- database - MongoDB传感器存储策略
- vector - 为什么模拟时会得到未定义的输出向量和未定义的时钟?
- rust - 在 Rust 中实现读取行或睡眠定时器有哪些好方法
- firebase - 无法将某些特定子域用于 firebase 动态链接