【问题标题】:Unrecognized angular component "app-navbar" while running ng-test运行 ng-test 时无法识别的角度组件“app-navbar”
【发布时间】:2018-09-16 17:32:13
【问题描述】:

失败:模板解析错误:“app-navbar”不是已知元素

  1. 如果 'app-navbar' 是一个 Angular 组件,则验证它是该模块的一部分。

  2. 如果“app-navbar”是 Web 组件,则将“CUSTOM_ELEMENTS_SCHEMA”添加到该组件的“@NgModule.schemas”以禁止显示此消息:

("[错误 ->]"): ng:///DynamicTestModule/AppComponent.html@0:0 错误:模板解析错误:

【问题讨论】:

    标签: javascript angular unit-testing typescript


    【解决方案1】:

    这些错误通常意味着您没有在模块中声明组件。 您需要在测试模块中包含app-navbar,否则,父组件不知道其子组件的存在,因此 - 它是“未知元素”。

    在此处查看有关模块的更多信息:https://angular.io/guide/ngmodules

    // Import it in your test
    import { NavBar } from '<location>';
    
    beforeEach(() => {
      TestBed.configureTestingModule({
        ...,
        // Add the child element to the declarations
        declarations: [ Navbar ],
        ...
      });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-13
      • 1970-01-01
      • 2019-03-29
      • 1970-01-01
      • 2017-06-09
      • 1970-01-01
      • 1970-01-01
      • 2020-10-17
      相关资源
      最近更新 更多