在为Angular 2应用编写测试时,我遇到了以下错误:我们正在使用的选择器:

"): AppComponent@12:35 'tab-view' is not a known element:
1. If 'my-tab' is an Angular component, then verify that it is part of this module.
2. If 'my-tab' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schemas' of this component to suppress this message. ("
    </div>
    <div class="app-body">
        [ERROR ->]<tab-view class="my-tab" [options]="tabOptions"></tab-view>
    </div> </div>

我已经将 CUSTOM_ELEMENTS_SCHEMA 和所有其他模块添加到了我的根模块中,但是仍然出现错误。
  • 我还需要做什么?
  • 这是否需要存在于所有模块中,还是仅存在于根目录中?
  • 还有什么我需要添加的吗?
  • 最佳答案

    所以我要做的就是在TestBed.configureTestingModule中设置模式-这不是一个单独的模块文件,而是app.component.spec.ts文件的一部分。感谢@camaron的提示。我确实认为文档在这一点上可能会更清晰。

    无论如何,这就是我添加的功能。这是我的app.component.spec.ts文件的开始内容。

    import { TestBed, async } from '@angular/core/testing';
    import { AppComponent } from './../app.component';
    import { RouterTestingModule } from '@angular/router/testing';
    import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
    
    describe('AppComponent', () => {
      beforeEach(() => {
        TestBed.configureTestingModule({
          schemas: [ CUSTOM_ELEMENTS_SCHEMA ],
          declarations: [AppComponent],
          imports: [RouterTestingModule]
        });
        TestBed.compileComponents();
      });
    

    10-05 21:08
    查看更多