简体   繁体   English

在父组件中使用子组件时出错 - Angular 2单元测试(KarmaJasmine)

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

template parse error when using child component in parent. 在父级中使用子组件时template parse error

I have two components. 我有两个组成部分。

  1. CardComponent
  2. InfoComponent

My CardComponent's HTML 我的CardComponent的HTML

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

CardComponent's Spec File: CardComponent的规范文件:

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

My InfoComponent ts code: 我的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 spec declaration: InfoComponent规范声明:

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

When I run test case for these two components individually then it is working fine. 当我单独运行这两个组件的测试用例时,它工作正常。

When I am running these test cases at a time then I am getting error: 当我一次运行这些测试用例时,我收到错误:

    '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>

I have imported InfoComponent in CardComponent even then I am getting this error. 我已经在CardComponent导入了InfoComponent ,即使这样我也收到了这个错误。 I am not able to find the issue. 我无法找到问题。 I any one knows then it would be great for me. 我知道那对我来说会很棒。

Do both components share the exact same Module? 这两个组件是否共享完全相同的模块? Is the module imported well? 模块导入好吗? Is forRoot() extrictly necessary? forRoot()非常必要?

From documentation : 来自文档

Call forRoot only in the root application module, AppModule. 仅在根应用程序模块AppModule中调用。 Calling it in any other module, particularly in a lazy-loaded module, is contrary to the intent and can produce a runtime error. 在任何其他模块中调用它,特别是在延迟加载的模块中,与意图相反,并且可能产生运行时错误。

Remember to import the result; 记得导入结果; don't add it to any other @NgModule list. 不要将它添加到任何其他@NgModule列表。

Try removing forRoot() and see if the error persists. 尝试删除forRoot()并查看错误是否仍然存在。

Hope I was of help, let me know how it goes! 希望我有所帮助,让我知道它是怎么回事!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM