【问题标题】:Angular 2 + ng test: 'X' is not a known component [duplicate]Angular 2 + ng 测试:“X”不是已知组件[重复]
【发布时间】:2017-02-10 03:24:00
【问题描述】:

我使用 Angular 2.0 RTM 和 Angular CLI 1.0.0-beta 16 创建了一个新项目:

ng init

然后跑:

ng generate component my-new-component

然后跑:

ng test

得到了这个:

'app-my-new-component' is not a known element:
1. If 'app-my-new-component' is an Angular component, then verify that it is part of this module.
2. If 'app-my-new-component' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schema' of this component to suppress this message. ("
</h1>
<p>The mean of life is {{answer}}</p>
[ERROR ->]<app-my-new-component></app-my-new-component>"): AppComponent@4:0
Error: Template parse errors:
    at TemplateParser.parse (http://localhost:9877/_karma_webpack_/0.bundle.js:8669:19)
    at RuntimeCompiler._compileTemplate (http://localhost:9877/_karma_webpack_/0.bundle.js:17854:51)
    at http://localhost:9877/_karma_webpack_/0.bundle.js:17777:83
    at Set.forEach (native)
    at compile (http://localhost:9877/_karma_webpack_/0.bundle.js:17777:47)
    at RuntimeCompiler._compileComponents (http://localhost:9877/_karma_webpack_/0.bundle.js:17779:13)
    at RuntimeCompiler._compileModuleAndAllComponents (http://localhost:9877/_karma_webpack_/0.bundle.js:17696:37)
    at RuntimeCompiler.compileModuleAndAllComponentsSync (http://localhost:9877/_karma_webpack_/0.bundle.js:17684:21)
    at TestingCompilerImpl.compileModuleAndAllComponentsSync (http://localhost:9877/_karma_webpack_/0.bundle.js:24662:35)
    at TestBed._initIfNeeded (webpack:///home/adam/monctonug/~/@angular/core/bundles/core-testing.umd.js:1059:0 <- src/test.ts:4568:40)

我尝试重新排序组件,但没有帮助。解决此问题的正确方法是什么?

【问题讨论】:

  • 有同样的问题。我在 GitHub 上查看了与此错误相关的打开/关闭问题,但没有找到解决方案。
  • 我为这个问题提供了关于同一问题的另一个问题的最新答案:stackoverflow.com/a/43998532/2209463

标签: angular webpack angular-cli


【解决方案1】:

我也遇到了同样的问题,结果证明你还需要声明使用过的组件:

  beforeEach(() => {
    TestBed.configureTestingModule({
      declarations: [
        AppComponent,
        MyNewComponent       
      ],
    });

【讨论】:

  • 这就是问题所在。我还有其他问题,但这解决了这个问题。
  • 我的生活中也有其他问题,这个答案没有解决,但它确实为我解决了这个问题!干得好。
  • 花了 30 分钟后,我找到了你的答案。谢谢!
猜你喜欢
  • 2017-11-09
  • 1970-01-01
  • 1970-01-01
  • 2019-01-22
  • 2023-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-02
相关资源
最近更新 更多