【发布时间】:2018-07-17 19:22:59
【问题描述】:
我在某些组件上运行 Angular 测试时遇到了一些问题,但这些组件在生产环境中运行良好。我不断收到此错误消息:
Failed: Template parse errors:
'app-date-picker' is not a known element:
1. If 'app-date-picker' is an Angular component, then verify that it is part of this module.
2. If 'app-date-picker' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. ("
</nav>
</div>
[ERROR ->]<app-date-picker></app-date-picker>
<div class="clearfix"></div>
</div>
我这里只有一个模块:
import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { AppComponent } from './app.component';
import { DatePickerComponent } from './date-picker/date-picker.component';
@NgModule({
imports: [
...
],
declarations: [
AppComponent,
DatePickerComponent,
...
],
schemas: [CUSTOM_ELEMENTS_SCHEMA],
bootstrap: [AppComponent]
})
我的组件如下所示:
@Component({
selector: 'app-date-picker',
templateUrl: './date-picker.component.html',
styleUrls: ['./date-picker.component.scss']
})
export class DatePickerComponent implements OnInit {
constructor(private dateService: DateService) {
this.startDateRange = new FormControl(this.dateService.startDateRange);
this.endDateRange = new FormControl(this.dateService.endDateRange);
}
ngOnInit() {...}
updateDateService() {...}
}
我可以通过将schemas: [NO_ERRORS_SCHEMA] 添加到TestBed.configureTestingModule 对象来消除这些错误中的一些,但是它们被替换为[object ErrorEvent] thrown 错误消息,这对我没有帮助丝毫。
有谁知道我该如何解决这个问题?如果我需要提供更多信息,请告诉我,谢谢!
编辑:
这是失败的组件之一的规范文件:
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { DatePickerComponent } from '../date-picker/date-picker.component';
import { MyComponent } from './my-github.component';
describe('MyComponent', () => {
let component: MyComponent;
let fixture: ComponentFixture<MyComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
MyComponent,
DatePickerComponent
],
// schemas: [NO_ERRORS_SCHEMA]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(MyComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
【问题讨论】:
-
能否添加您的规范文件?
-
添加了规范文件。谢谢!
-
MyComponent和DatePickerComponent是否在不同的模块中声明? -
@amit 只有一个模块,贴在上面。
标签: angular angular-cli karma-jasmine