Angular5/Karma 'selector' 不是已知元素
Angular5/Karma 'selector' is not a known element
我的组件有一个小问题。在我的浏览器中一切正常,没有任何错误,但 Karma 调试器抛出一些错误,就我想弄清楚一切而言,我想修复它。
错误:
Failed: Template parse errors:
'dj-menu' is not a known element:
1. If 'dj-menu' is an Angular component, then verify that it is part of this module.
2. If 'dj-menu' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.
<dj-menu>
选择器用于 app.component.html
。
menu.component.ts
import {Component, OnInit} from '@angular/core';
@Component({
selector: 'dj-menu',
template: `<nav>
<button mat-button routerLink="/login" routerLinkActive="active">Login</button>
<button mat-button routerLink="/register" routerLinkActive="active">Register</button>
<button mat-button routerLink="/profile" routerLinkActive="active">Profile</button>
<button mat-button routerLink="/radio">Radio</button>
</nav>
`,
styleUrls: ['./menu.component.scss']
})
export class MenuComponent implements OnInit {
{...} // only an empty constructor and ngOnInit()
}
我在 app.module.ts.
的 @NgModule
中的 declarations
中声明了一个 MenuComponent
正如我所说,它在浏览器中运行良好,但在 Karma 调试器中却不行。到目前为止,我尝试了一些关于类似错误的答案(例如 ),但没有任何效果。
感谢任何帮助,谢谢。
当你测试父组件时你需要存根子组件,例如我有两个组件一个是app
另一个是dj-menu
然后我应该在[中模拟dj-menu
=13=] 使用如下代码进行测试
@Component({
selector: 'dj-menu',
template: '<h1> Some test </h1>'
})
class DjMenuComponentStub{
}
我的应用程序组件如下所示(使用 dj-menu
)
@Component({
selector: 'app',
template: '<dj-menu></dj-menu>'
})
class AppComponent{
}
在测试应用程序组件时,angular 不知道 dj-menu
您必须通过声明
告诉 angular 使用存根组件
TestBed.configureTestingModule({
declarations: [ DjMenuComponentStub ]
})
我的组件有一个小问题。在我的浏览器中一切正常,没有任何错误,但 Karma 调试器抛出一些错误,就我想弄清楚一切而言,我想修复它。
错误:
Failed: Template parse errors:
'dj-menu' is not a known element:
1. If 'dj-menu' is an Angular component, then verify that it is part of this module.
2. If 'dj-menu' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.
<dj-menu>
选择器用于 app.component.html
。
menu.component.ts
import {Component, OnInit} from '@angular/core';
@Component({
selector: 'dj-menu',
template: `<nav>
<button mat-button routerLink="/login" routerLinkActive="active">Login</button>
<button mat-button routerLink="/register" routerLinkActive="active">Register</button>
<button mat-button routerLink="/profile" routerLinkActive="active">Profile</button>
<button mat-button routerLink="/radio">Radio</button>
</nav>
`,
styleUrls: ['./menu.component.scss']
})
export class MenuComponent implements OnInit {
{...} // only an empty constructor and ngOnInit()
}
我在 app.module.ts.
的@NgModule
中的 declarations
中声明了一个 MenuComponent
正如我所说,它在浏览器中运行良好,但在 Karma 调试器中却不行。到目前为止,我尝试了一些关于类似错误的答案(例如
感谢任何帮助,谢谢。
当你测试父组件时你需要存根子组件,例如我有两个组件一个是app
另一个是dj-menu
然后我应该在[中模拟dj-menu
=13=] 使用如下代码进行测试
@Component({
selector: 'dj-menu',
template: '<h1> Some test </h1>'
})
class DjMenuComponentStub{
}
我的应用程序组件如下所示(使用 dj-menu
)
@Component({
selector: 'app',
template: '<dj-menu></dj-menu>'
})
class AppComponent{
}
在测试应用程序组件时,angular 不知道 dj-menu
您必须通过声明
TestBed.configureTestingModule({
declarations: [ DjMenuComponentStub ]
})