我创建了一个名为 的自定义组件core.component
,并将其导入到同名的模块中。
core.component.html
<div class="container-fluid text-left">
<div class="row rows-cols-2 top-bar">
<div>
<h1>[Title]</h1>
</div>
<div class="w-100"></div>
<div>
<h4>[Subtitle]</h4>
</div>
</div>
<div class="row align-baseline side-panel">
<div class="col-3">
<nav class="navbar bg-body-tertiary">
<a class="navbar-brand" href="#">Option 1</a>
</nav>
<nav class="navbar bg-body-tertiary">
<a class="navbar-brand" href="#">Option 2</a>
</nav>
</div>
<div class="col-9">
</div>
</div>
</div>
核心组件.ts
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-core',
templateUrl: './core.component.html',
styleUrls: ['./core.component.scss']
})
export class CoreComponent implements OnInit {
constructor() { }
ngOnInit(): void {
}
}
核心模块.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { CoreComponent } from './core.component';
@NgModule({
declarations: [
CoreComponent
],
imports: [
CommonModule
]
})
export class CoreModule { }
最后,我将核心模块导入到我的根应用程序模块中,以便我可以在元素中显示它<app-root>
。
应用程序模块.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { CoreModule } from './core/core.module';
@NgModule({
declarations: [
AppComponent,
],
imports: [
BrowserModule,
AppRoutingModule,
CoreModule ],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
应用程序组件.html
<app-core></app-core>
不幸的是,编译器没有渲染核心组件 HTML,而是返回以下消息,指出它无法识别我通过核心模块导入的组件。
Error: src/app/app.component.html:2:1 - error NG8001: 'app-core' is not a known element:
1. If 'app-core' is an Angular component, then verify that it is part of this module.
2. If 'app-core' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.
2 <app-core></app-core>
~~~~~~~~~~
您需要
CoreComponent
从core.module.ts “导出” :代码沙箱