当我在 Angular 5 中的两个组件之间实现ng-content时,我遇到了奇怪的问题。
这是我的第一个组件是 FooterComponent
<div class="footer">
<ng-content select="footer"></ng-content>
</div>
和 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() {
}
}
这第二个组件是 AboutComponent
<app-footer>
<footer>
this is my footer
</footer>
</app-footer>
这个 app-module.ts 在第一次尝试和第二次尝试中我导入 about.module.ts 或 footer.module.ts 而不是 AboutComponent 和 FooterComponent。
import { BrowserModule } from '@angular/platform-browser';
import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { AppComponent } from './app.component';
import { AboutComponent } from './about/about.component';
import { FooterComponent } from './footer/footer.component';
@NgModule({
declarations: [
AppComponent,
AboutComponent,
FooterComponent
],
imports: [
BrowserModule,
],
providers: [],
bootstrap: [AppComponent],
schemas: [ CUSTOM_ELEMENTS_SCHEMA ]
})
export class AppModule { }
app.component.html 只有 router-outlet 标签
<router-outlet></router-outlet>
所以我有两个问题,第一个是当我使用ng serve命令行运行 Angular 时,我没有看到任何输出。第二个也是重要的一个是当我运行ng test命令行时,我看到了这个错误......
AboutComponent should create
Failed: Template parse errors:
'app-footer' is not a known element:
1. If 'app-footer' is an Angular component, then verify that it is part of
this module.
2. If 'app-footer' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to
the '@NgModule.schemas' of this component to suppress this message. ("
[ERROR ->]<app-footer>
<footer>
this is my footer
"): ng:///DynamicTestModule/AboutComponent.html@0:0
所以我一直在尝试很多解决方案,比如
生成 about.module.ts 并导入CUSTOM_ELEMENTS_SCHEMA并尝试NO_ERRORS_SCHEMA。并且还导入FooterComponent,最后在app.module.ts中导入about.module.ts
我对 FooterComponent 做同样的事情。
我一直在尝试在每个模块中使用CUSTOM_ELEMENTS_SCHEMA和NO_ERRORS_SCHEMA,这给了我相同的结果。
而这个 package.json
{
"name": "ng-app",
"version": "0.0.0",
"license": "MIT",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build --prod",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
"private": true,
"dependencies": {
"@angular/animations": "^5.2.0",
"@angular/common": "^5.2.0",
"@angular/compiler": "^5.2.0",
"@angular/core": "^5.2.0",
"@angular/forms": "^5.2.0",
"@angular/http": "^5.2.0",
"@angular/platform-browser": "^5.2.0",
"@angular/platform-browser-dynamic": "^5.2.0",
"@angular/router": "^5.2.0",
"core-js": "^2.4.1",
"rxjs": "^5.5.6",
"zone.js": "^0.8.19"
},
"devDependencies": {
"@angular/cli": "~1.7.3",
"@angular/compiler-cli": "^5.2.0",
"@angular/language-service": "^5.2.0",
"@types/jasmine": "~2.8.3",
"@types/jasminewd2": "~2.0.2",
"@types/node": "~6.0.60",
"codelyzer": "^4.0.1",
"jasmine-core": "~2.8.0",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~2.0.0",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "^1.2.1",
"karma-jasmine": "~1.1.0",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "~5.1.2",
"ts-node": "~4.1.0",
"tslint": "~5.9.1",
"typescript": "~2.5.3"
}
}
最后,我创建了这个新的 Angular-cli 项目。
谢谢。