"CUSTOM_ELEMENTS_SCHEMA"测试Angular 2 App时出错 [英] "CUSTOM_ELEMENTS_SCHEMA" Errors in Testing Angular 2 App

查看:686
本文介绍了"CUSTOM_ELEMENTS_SCHEMA"测试Angular 2 App时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In writing tests for my Angular 2 app, I am running into these errors: the selectors we're using:

"): 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 ,但我仍然遇到错误.

I have added CUSTOM_ELEMENTS_SCHEMA to my root module, as well as all other modules, but I am still getting the errors.

  • 我还需要做什么?
  • 这是否需要存在于所有模块中,还是仅存在于根目录中?
  • 还有什么需要补充的吗?

推荐答案

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

So what I had to do to get this working was also set the schemas in the TestBed.configureTestingModule - which is not a separate module file, but a part of the app.component.spec.ts file. Thanks to @camaron for the tip. I do think the docs could be a clearer on this point.

无论如何,这就是我为了使其正常工作而添加的内容.这是我的app.component.spec.ts文件的开头内容.

Anyway, this is what I added to get it to work. Here are the opening contents of my app.component.spec.ts file.

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();
  });

这篇关于"CUSTOM_ELEMENTS_SCHEMA"测试Angular 2 App时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆