簡體   English   中英

在父組件中使用子組件時出錯 - Angular 2單元測試(KarmaJasmine)

[英]Error while using child component in parent component - Angular 2 Unit Testing (KarmaJasmine)

在父級中使用子組件時template parse error

我有兩個組成部分。

  1. CardComponent
  2. InfoComponent

我的CardComponent的HTML

     <div class="footer">
       <full-info></full-info>
     </div>

CardComponent的規范文件:

 beforeEach(async(() => {
TestBed.configureTestingModule({
  declarations: [InfoComponent, CardComponent ],
  imports: [
      FormsModule,
      MaterialModule.forRoot()
  ],
  schemas: [ NO_ERRORS_SCHEMA ], //Used CUSTOM_ERRORS_SCHEMA also
})
.compileComponents();
  }));

我的InfoComponent ts代碼:

   import { Component, Input } from '@angular/core';


   @Component({
            selector: 'full-info',
            templateUrl: './full-info.component.html',
            styleUrls: ['./full-info.component.css']
            })

   export class InfoComponent {
     public fullInfo;
    }

InfoComponent規范聲明:

     beforeEach(async(() => {
TestBed.configureTestingModule({
  declarations: [ InfoComponent ],
  imports: [
      FormsModule,
      MaterialModule.forRoot()
  ],
  schemas: [ NO_ERRORS_SCHEMA ]
})
.compileComponents();
   }));

當我單獨運行這兩個組件的測試用例時,它工作正常。

當我一次運行這些測試用例時,我收到錯誤:

    'full-info' is not a known element:
1. If 'full-info' is an Angular component, then verify that it is part of this module.
2. If 'full-info' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schemas' of this component to suppress this message. ("
  </div>
  <div class="footer">
    [ERROR ->]<full-info></full-info>
  </div>

我已經在CardComponent導入了InfoComponent ,即使這樣我也收到了這個錯誤。 我無法找到問題。 我知道那對我來說會很棒。

這兩個組件是否共享完全相同的模塊? 模塊導入好嗎? forRoot()非常必要?

來自文檔

僅在根應用程序模塊AppModule中調用。 在任何其他模塊中調用它,特別是在延遲加載的模塊中,與意圖相反,並且可能產生運行時錯誤。

記得導入結果; 不要將它添加到任何其他@NgModule列表。

嘗試刪除forRoot()並查看錯誤是否仍然存在。

希望我有所幫助,讓我知道它是怎么回事!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM