Angular 角度6:失败:模板分析错误:Can';t绑定到';routerLink';因为它不是';t'的已知属性;a';。(

Angular 角度6:失败:模板分析错误:Can';t绑定到';routerLink';因为它不是';t'的已知属性;a';。(,angular,typescript,unit-testing,bootstrap-4,karma-jasmine,Angular,Typescript,Unit Testing,Bootstrap 4,Karma Jasmine,我正在使用npm测试测试我的应用程序 我得到以下错误: Failed: Template parse errors: Can't bind to 'routerLink' since it isn't a known property of 'a'. (" <nav> <ul> <li><a class="nav-link active" [ERROR ->][routerLink]="['/movies']"

我正在使用
npm测试测试我的应用程序

我得到以下错误:

Failed: Template parse errors:
Can't bind to 'routerLink' since it isn't a known property of 'a'. ("
    <nav>
      <ul>
        <li><a class="nav-link active" [ERROR ->][routerLink]="['/movies']" routerLinkActive="active-link" [routerLinkActiveOptions]="{exact: true}" c"): ng:///DynamicTestModule/AppComponent.html@4:39
Can't bind to 'routerLinkActiveOptions' since it isn't a known property of 'a'. ("l>
        <li><a class="nav-link active" [routerLink]="['/movies']" routerLinkActive="active-link" [ERROR ->][routerLinkActiveOptions]="{exact: true}" class="fa fa-home"><span>Home</span></a></li>
        <li><"): ng:///DynamicTestModule/AppComponent.html@4:97
Can't bind to 'routerLink' since it isn't a known property of 'a'. ("uterLinkActiveOptions]="{exact: true}" class="fa fa-home"><span>Home</span></a></li>
        <li><a [ERROR ->][routerLink]="['/movies']" class="fa fa-film"><span>Best Movies 200's</span></a></li>
        <li><a "): ng:///DynamicTestModule/AppComponent.html@5:15
Can't bind to 'routerLink' since it isn't a known property of 'a'. ("routerLink]="['/movies']" class="fa fa-film"><span>Best Movies 200's</span></a></li>
        <li><a [ERROR ->][routerLink]="['/theatre']" class="fa fa-television"><span>Movies in Theatre</span></a></li>
        "): ng:///DynamicTestModule/AppComponent.html@6:15
Can't bind to 'routerLink' since it isn't a known property of 'a'. ("ink]="['/theatre']" class="fa fa-television"><span>Movies in Theatre</span></a></li>
        <li><a [ERROR ->][routerLink]="['/tvshows']" class="fa fa-video-camera"><span>Tv Shows</span></a></li>
        <li><a "): ng:///DynamicTestModule/AppComponent.html@7:15
'router-outlet' is not a known element:
1. If 'router-outlet' is an Angular component, then verify that it is part of this module.
2. If 'router-outlet' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. ("

<div class="container">
  [ERROR ->]<router-outlet></router-outlet>
</div>"): ng:///DynamicTestModule/AppComponent.html@15:2
这是我为路由应用程序路由模块定制的模块

import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { MoviesComponent } from '../movies/movies.component';
import { TheatreComponent } from '../theatre/theatre.component';
import { TvShowsComponent } from '../tv-shows/tv-shows.component';
import { CommonModule } from '@angular/common';
const routes: Routes = [
  {
    path: 'movies',
    component: MoviesComponent
  },
  {
    path: 'theatre',
    component: TheatreComponent
  },
  {
    path: 'tvshows',
    component: TvShowsComponent
  },
  {
    path: '',
    redirectTo: 'movies',
    pathMatch: 'full'
  }
];

@NgModule({
  imports: [CommonModule, RouterModule.forRoot(routes)],
  exports: [RouterModule],
  schemas: [ CUSTOM_ELEMENTS_SCHEMA ]
})
export class AppRoutingModule { }
**

更新

**

这是testspec.ts文件

import { TestBed, async } from '@angular/core/testing';
import { AppComponent } from './app.component';
describe('AppComponent', () => {
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [
        AppComponent
      ],
    }).compileComponents();
  }));
  it('should create the app', async(() => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.debugElement.componentInstance;
    expect(app).toBeTruthy();
  }));
  it(`should have as title 'app'`, async(() => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.debugElement.componentInstance;
    expect(app.title).toEqual('app');
  }));
  it('should render title in a h1 tag', async(() => {
    const fixture = TestBed.createComponent(AppComponent);
    fixture.detectChanges();
    const compiled = fixture.debugElement.nativeElement;
    expect(compiled.querySelector('h1').textContent).toContain('Welcome to movies-client!');
  }));
});
注意:我试图向app component.spec.ts添加
CUSTOM\u ELEMENTS\u SCHEMA
,但仍然出现了相同的错误,尝试了不同的组合,但我在单元测试中没有遗漏什么?或者什么

问题


我的代码有什么问题?

您应该在测试规范中导入
RouterTestingModule

TestBed.configureTestingModule({
    .......
     imports: [
         RouterTestingModule,
         ......
     ],
     schemas: [ CUSTOM_ELEMENTS_SCHEMA ],
     ......
})

你在测试文件中导入了吗?
我试图将自定义元素模式添加到app component.spec.ts中,但仍然出现同样的错误,
你的测试规范文件在哪里?你正在将它添加到
app.routing.module
。显示你的测试规范文件。alt阅读问题ain app module.ts是否包含在内,请检查,阿米特·奇加达尼这是我在angukar 6中做单元测试,他只是在angular 2中做基本的路由!steve不,我不是在学习这个单元测试,所以你知道有很多东西需要学习heheYes,这不是完全重复的。我已经删除了我的评论。但你可能没有在tes中导入
RouterModule
t spec.检查答案。对于测试,您可以使用
RouterTestingModule
。在我的应用程序中,我没有看到test.spec.ts我只是有test.ts angular 6很多东西改变了想法,所以没有编写测试规范,您尝试运行测试用例。奇怪。如果您只是运行
npm test
ng test 。这显然会给你带来错误,因为你没有配置你的
app.component.spec
test.ts
不是你的规范,它只是测试环境的整体配置。我已经更新了qn,我已经添加了app.component,spec.ts。现在检查它很好,现在导入
RouterTestingModule
,如图所示。此外,您应该在必要时导入spec文件中的其他模块。请完整阅读教程,然后开始测试。
import { TestBed, async } from '@angular/core/testing';
import { AppComponent } from './app.component';
describe('AppComponent', () => {
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [
        AppComponent
      ],
    }).compileComponents();
  }));
  it('should create the app', async(() => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.debugElement.componentInstance;
    expect(app).toBeTruthy();
  }));
  it(`should have as title 'app'`, async(() => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.debugElement.componentInstance;
    expect(app.title).toEqual('app');
  }));
  it('should render title in a h1 tag', async(() => {
    const fixture = TestBed.createComponent(AppComponent);
    fixture.detectChanges();
    const compiled = fixture.debugElement.nativeElement;
    expect(compiled.querySelector('h1').textContent).toContain('Welcome to movies-client!');
  }));
});
TestBed.configureTestingModule({
    .......
     imports: [
         RouterTestingModule,
         ......
     ],
     schemas: [ CUSTOM_ELEMENTS_SCHEMA ],
     ......
})