2016-10-01 38 views
15

我創建與角2.0 RTM一個新的項目與角CLI 1.0.0-β16:角2 +納克測試: 'X' 是不是已知的部件

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>"): [email protected]: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) 

我試圖重新排序的部件,並沒有幫助。解決這個問題的正確方法是什麼?

+0

具有相同的問題。我在GitHub上發現了與此錯誤相關的開放/關閉問題,但未找到解決此問題的解決方案。 – ajurasz

回答

25

我面臨同樣的問題,並把它轉化出來,對於測試需要聲明所使用的組件還有:

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

這是問題所在。我還有其他問題,但是這解決了這個問題。 –

+0

我也有我生活中的其他問題,這個答案沒有解決,但它確實爲我解決了這個問題!工作很好。 –