失败:模板解析错误:“ router-outlet”不是已知元素:
如果“ router-outlet”是Angular组件,则请确认它是该模块的一部分。
If 'router-outlet' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. ("[ERROR ->]<router-outlet></router-outlet>"): ng:///DynamicTestModule/***.html@0:0
angular 4 ng测试失败:模板解析错误:“ router-outlet”不是已知元素
尝试以下操作:在(app.component.spec.ts)中,将导入:[RouterTestingModule]从@ angular / router / testing 添加到您的TestBed中,如下所示:
import { RouterTestingModule } from '@angular/router/testing'; // <= here
...
describe('AppComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [RouterTestingModule], // <= and here
declarations: [
AppComponent,
],
}).compileComponents();
}));
});
Run Code Online (Sandbox Code Playgroud)
或
有时,只需一行代码即可解决此问题。添加:
exports:[ RouterModule ],
到您的routing.module.ts的@NgModule中,它将解决此问题。这是我的routing.module.ts,我必须手动添加它,因为当我执行gm routing时,Angular CLI没有添加它。
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { Routes, RouterModule } from '@angular/router';
const routes: Routes =
[
// Your paths to the components
]
@NgModule({
imports: [
CommonModule,
RouterModule.forRoot(routes),
],
declarations: [],
exports: [ RouterModule ], // <= Add this line
})
export class ComponentRoutingModule { }
Run Code Online (Sandbox Code Playgroud)
将RoutingModule导入到您的app.module.ts中。
希望能帮助到你。
归档时间: |
|
查看次数: |
1265 次 |
最近记录: |