简体   繁体   English

许多失败:模板解析错误:无法绑定到“routerLink”,因为它不是“a”的已知属性。 Karma 中的错误

[英]Lots of Failed: Template parse errors: Can't bind to 'routerLink' since it isn't a known property of 'a'. errors in Karma

Failed: Template parse errors:
Can't bind to 'routerLink' since it isn't a known property of 'a'. ("nd">
            <img width="25" src="../assets/images/app_logo.jpg">
            <a class="navbar-brand" [ERROR ->][routerLink]="['/avior/dashboard']">&nbsp;&nbsp;=== Application ===</a>
        </a>

"): ng:///DynamicTestModule/AppComponent.html@7:27
...
NullInjectorError: StaticInjectorError[AviorBackendService]: 
  NullInjectorError: No provider for AviorBackendService!
...
Failed: Unexpected pipe 'SearchPipe' imported by the module 'DynamicTestModule'. Please add a @NgModule annotation.
...
Expected undefined to be truthy.

The same error goes over and over again for every a anchor with [routerLink] inside of it...对于每个包含 [routerLink] 的锚点,同样的错误一遍又一遍......

I tried importing RouterModule into app.component.ts AND app.module.ts, but it does not seem to help.我尝试将 RouterModule 导入 app.component.ts AND app.module.ts,但似乎没有帮助。 I also imported it into every .ts file where the routerLink is used but that didn't help either.我还将它导入到使用 routerLink 的每个 .ts 文件中,但这也无济于事。

I tried importing import { RouterTestingModule } from '@angular/router/testing';我尝试import { RouterTestingModule } from '@angular/router/testing';导入import { RouterTestingModule } from '@angular/router/testing'; in the spec.ts and that didn't help either but I think it's somewhere in the right way.在 spec.ts 中,这也无济于事,但我认为这是正确的方式。

PS I haven't changed the .spec.ts files with a single exception. PS 我没有更改 .spec.ts 文件,只有一个例外。

UPDATE更新

My dashboard component spec.ts:我的仪表板组件规格:

import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { DashboardComponent } from './dashboard.component';

describe('DashboardComponent', () => {
  let component: DashboardComponent;
  let fixture: ComponentFixture<DashboardComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [ DashboardComponent ]
    })
    .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(DashboardComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });
});

My Login Component spec.ts:我的登录组件规范:

import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { LoginComponent } from './login.component';

describe('LoginComponent', () => {
  let component: LoginComponent;
  let fixture: ComponentFixture<LoginComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [ LoginComponent ]
    })
    .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(LoginComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });
});

As you can see they're default tests.如您所见,它们是默认测试。

At AppComponent.html@7:27 the problem was indeed nested a anchors:在 AppComponent.html@7:27 问题确实嵌套了一个锚点:

<a class="navbar-brand">
            <img width="25" src="../assets/images/app_logo.jpg">
            <a class="navbar-brand" [routerLink]="['/avior/dashboard']">&nbsp;&nbsp;=== Application ===</a>
    </a>

I changed the outer a anchor to div, but the other errors still persist.我将外部 a 锚点更改为 div,但其他错误仍然存​​在。

The real RouterLinkDirective is quite complicated and entangled with other components and directives of the RouterModule .真正的RouterLinkDirective非常复杂,并且与RouterModule其他组件和指令纠缠在一起。 It requires challenging setup to mock and use in tests.它需要具有挑战性的设置来模拟和在测试中使用。

The RouterLinkDirectiveStub in this sample code replaces the real directive with an alternative version designed to validate the kind of anchor tag wiring seen in the AppComponent template (For example).此示例代码中的RouterLinkDirectiveStub用替代版本替换真实指令,该版本旨在验证AppComponent template (例如)中看到的锚标记接线类型。

For more information, visit the angular testing docs有关更多信息,请访问角度测试文档

For those interested the solution was the following - I needed to import RouterTestingModule (and also ReactiveFormsModule and HttpClientTestingModule) and in imports specify the routes like the following:对于那些感兴趣的人,解决方案如下 - 我需要导入 RouterTestingModule(以及 ReactiveFormsModule 和 HttpClientTestingModule)并在导入中指定如下路由:

imports: [ ReactiveFormsModule, HttpClientTestingModule, RouterTestingModule.withRoutes(
        [{path: '', redirectTo: 'avior/login', pathMatch: 'full'},

        {path: 'avior', loadChildren: () => import('../avior.module').then(m => m.AviorModule)},
        {path: 'avior/*', redirectTo: '/avior'},

        {path: 'carinae', loadChildren: () => import('../../carinae/carinae.module').then(m => m.CarinaeModule)},
        {path: 'carinae/*', redirectTo: '/carinae'},

        {path: '**', redirectTo: '/avior/404'}]
      ) ],

For为了

NullInjectorError: StaticInjectorError[AviorBackendService]: 
  NullInjectorError: No provider for AviorBackendService!

I needed to declare the BackendService as a provider and also import HttpClientTestingModule.我需要将 BackendService 声明为提供者并导入 HttpClientTestingModule。

失败:模板解析错误:无法绑定到“ngModel”,因为它不是“输入”的已知属性。 ("<div class="input-group"> *</div><div id="text_translate"><p> 更新到 angular 12 后,出现以下错误。</p><p> 项目正在构建和运行,但每当我尝试<strong>ng test</strong>进行单元测试时,我都会遇到以下错误</p><p><em>失败:模板解析错误:无法绑定到“ngModel”,因为它不是“输入”的已知属性。</em> <em>("</em></p><p> 我尝试<strong>从 '@angular/forms' 导入 import { FormsModule, ReactiveFormsModule };</strong> 但仍然是同样的错误。 请帮我解决这个问题。</p></div> - Failed: Template parse errors: Can't bind to 'ngModel' since it isn't a known property of 'input'. (" <div class="input-group">*

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 模板解析错误:无法绑定到&#39;routerLink&#39;,因为它不是&#39;a&#39;的已知属性 - Template parse errors: Can't bind to 'routerLink' since it isn't a known property of 'a' 失败:模板解析错误:无法绑定到“routerLink”,因为它不是“a”的已知属性。 (“s=”导航栏导航“&gt; - Failed: Template parse errors: Can't bind to 'routerLink' since it isn't a known property of 'a'. (“s=”navbar-nav" > 错误:模板解析错误:无法绑定到“ ngForOf”,因为它不是“模板”的已知属性 - Error: Template parse errors: Can't bind to 'ngForOf' since it isn't a known property of 'template' 失败:模板解析错误:无法绑定到“ngModel”,因为它不是“输入”的已知属性。 ("<div class="input-group"> *</div><div id="text_translate"><p> 更新到 angular 12 后,出现以下错误。</p><p> 项目正在构建和运行,但每当我尝试<strong>ng test</strong>进行单元测试时,我都会遇到以下错误</p><p><em>失败:模板解析错误:无法绑定到“ngModel”,因为它不是“输入”的已知属性。</em> <em>("</em></p><p> 我尝试<strong>从 '@angular/forms' 导入 import { FormsModule, ReactiveFormsModule };</strong> 但仍然是同样的错误。 请帮我解决这个问题。</p></div> - Failed: Template parse errors: Can't bind to 'ngModel' since it isn't a known property of 'input'. (" <div class="input-group">* Angular2测试 - 失败:未捕获(在承诺中):错误:模板解析错误:无法绑定到“消息”,因为它不是已知的属性 - Angular2 Testing - Failed: Uncaught (in promise): Error: Template parse errors: Can't bind to 'message' since it isn't a known property of 无法导航:模板解析错误:无法绑定到“选项”,因为它不是“签名板”的已知属性 - Failed to navigate: Template parse errors: Can't bind to 'options' since it isn't a known property of 'signature-pad' Ngx无限滚动 - 模板解析错误:无法绑定到&#39;infiniteScrollDistance&#39;,因为它不是已知属性 - Ngx infinite scrolling - Template parse errors: Can't bind to 'infiniteScrollDistance' since it isn't a known property Angular中的错误:模板解析错误:无法绑定到&#39;datetime&#39;,因为它不是&#39;time&#39;的已知属性 - Error in Angular: Template parse errors: Can't bind to 'datetime' since it isn't a known property of 'time' 模板解析错误:无法绑定到“ngbTypeahead”,因为它不是“input”的已知属性 - Template parse errors: Can't bind to 'ngbTypeahead' since it isn't a known property of 'input' 错误:模板解析错误:无法绑定到“评级”,因为它不是“评级”的已知属性 - Error: Template parse errors: Can't bind to 'rating' since it isn't a known property of 'rating'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM