代码之家  ›  专栏  ›  技术社区  ›  jinfy

角度6业力测试“@input decorator

  •  1
  • jinfy  · 技术社区  · 6 年前

    在appcomponent中,我在html代码中使用nav组件。用户界面看起来很好。发球时没有失误。当我查看应用程序时,控制台没有错误。 但当我为我的项目运行业力时,有一个错误:

    错误:

    Failed: Template parse errors:
    Can't bind to 'head' since it isn't a known property of 'app-widget'.
    1. If 'app-widget' is an Angular component and it has 'head' input, then verify that it is part of this module.
    2. If 'app-widget' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.
    3. To allow any property add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component. ("<app-widget [ERROR ->][head]="heading"></app-widget>
    

    规格:

    import { async, ComponentFixture, TestBed } from '@angular/core/testing';
    import { HomeService } from './../../service/home.service';
    import { MyFavoriteComponent } from './my-favorite.component';
    
    describe('MyFavoriteComponent', () => {
      let component: MyFavoriteComponent;
      let fixture: ComponentFixture<MyFavoriteComponent>;
    
      beforeEach(async(() => {
        TestBed.configureTestingModule({
          declarations: [ MyFavoriteComponent ],
          providers: [{provide: HomeService}]
        })
        .compileComponents();
      }));
    
      beforeEach(() => {
        fixture = TestBed.createComponent(MyFavoriteComponent);
        component = fixture.componentInstance;
        fixture.detectChanges();
      });
    
      it('should create', () => {
        expect(component).toBeTruthy();
      });
    });
    

    电话号码:

    import { Component, OnInit, Input } from '@angular/core';
    import { HomeService } from './../../service/home.service';
    
    @Component({
      selector: 'app-my-favorite',
      templateUrl: './my-favorite.component.html',
      styleUrls: ['./my-favorite.component.scss']
    })
    export class MyFavoriteComponent implements OnInit {
      heading = '';
      constructor(private homeService: HomeService) {}
    
       ngOnInit() {
        this.homeService.homeDataWidget().subscribe(response => {
          this.heading = response.myfavoirte;
        });
    
       }
    
    }
    

    HTML格式

    <app-widget [head]="heading"></app-widget>
    
    1 回复  |  直到 5 年前
        1
  •  0
  •   Samoth    6 年前

    你的组件可能使用app widget。您应该将它添加到测试中的“声明”(如果它是组件)或“导入”(如果它是模块)。

    第二种方法是添加“无错误模式”:

    TestBed.configureTestingModule({
          imports: [FormsModule],
          declarations: [ MyFavoriteComponent, WidgetComponent],
          providers: [{provide: HomeService}],
          schemas: [NO_ERRORS_SCHEMA]
        })