相关疑难解决方法(0)

单元测试Angular与Jasmine和Karma,错误:无法绑定到'xxx',因为它不是'xxxxxx'的已知属性.

我对使用Jasmine和Karma的5角单元测试存在问题.

错误是:"无法绑定到'fruits',因为它不是'my-component2'的已知属性".

单元测试代码是:

SRC /应用/组件/我-COMPONENT1 /我-component1.component.spec.ts:

import { TestBed, inject, ComponentFixture, async} from '@angular/core/testing';
import {HttpClientTestingModule, HttpTestingController} from '@angular/common/http/testing';

import { Post } from '../../models/post.model';

import { MyComponent1Component } from './my-component1.component';
import { MyService1Service } from '../../services/my-service1.service';

import { HttpClientModule, HttpClient,HttpHandler } from '@angular/common/http';
import {fruit} from '../../Models/fruit';


fdescribe('MyComponent1Component', () => {
  let component: MyComponent1Component;
  let fixture: ComponentFixture<MyComponent1Component>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [ MyComponent1Component ],
      imports: [],
      providers: [MyService1Service, HttpClient, HttpHandler, HttpTestingController]
    })

    .compileComponents();
  }));

  beforeEach(async(() => {
    fixture …
Run Code Online (Sandbox Code Playgroud)

karma-jasmine angular

26
推荐指数
2
解决办法
1万
查看次数

错误:多个组件匹配带有标记名 app-contact 的节点

我有一个 Angular Material 应用程序,其中有一个卡片列表,每个卡片代表一个Contact. 即具有各种属性的对象,例如名字、姓氏、电子邮件、图像等。

要显示此列表,我只需遍历Contacts列表:

<mat-list role="list" class="contacts-container-box">
  <div class="application-box" role="listitem" *ngFor="let contact of contacts">
    <app-contact [contact]="contact"></app-contact>
  </div>
</mat-list>
Run Code Online (Sandbox Code Playgroud)

app-contact然后该组件负责显示每个Contact. 但是,如果我尝试按原样运行应用程序,则在编译时不会出错,但在浏览器控制台中,我得到以下信息: 错误

我试图在 StackBlitz 上重现这个问题,我得到了这个:

错误2

无论如何,它不会按预期工作。请注意,这个错误只有当我取消注释方法时发生getContactImage()ContactsService,如果我评论的一切作品除,当然,对于未加载的图像。

Stackblitz 演示:https ://stackblitz.com/edit/angular-ivy-tnb6rv

service angular-material angular

2
推荐指数
1
解决办法
4818
查看次数