繁体   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