[英]Angular2 material 'md-icon' is not a known element with Karma / Jasmine
我正在使用 @angular/material 2.0.0-alpha.11-3 angular-cli 1.0.0-beta.19-3 karma 1.2.0 karma-jasmine 1.0.2 開發 Angular2 應用程序
運行它工作正常,但模板有一個帶有 md-icon 的按鈕的幾個測試失敗並出現模板錯誤:
ERROR: 'Unhandled Promise rejection:', 'Template parse errors:
'md-icon' is not a known element:
1. If 'md-icon' is an Angular component, then verify that it is part of this module.
2. If 'md-icon' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schemas' of this component to suppress this message
我的 app.module.ts:
import { MaterialModule } from '@angular/material';
@NgModule({
declarations: [
AppComponent,
LinearProgressIndicatorComponent,
MyNewDirectiveDirective,
MyNewServiceDirective,
HeaderComponent,
MenuComponent,
WatchpanelComponent,
InputComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
NgbModule.forRoot(),
MaterialModule.forRoot(),
],
exports: [ MaterialModule ],
providers: [LocalStorage],
bootstrap: [AppComponent]
})
export class AppModule { }
watchpanel.component.spec.ts:
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { DebugElement } from '@angular/core';
import { WatchpanelComponent } from './watchpanel.component';
describe('WatchpanelComponent', () => {
let component: WatchpanelComponent;
let fixture: ComponentFixture<WatchpanelComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ WatchpanelComponent ] // declare the test component
})
.compileComponents();
fixture = TestBed.createComponent(WatchpanelComponent);
component = fixture.componentInstance;
fixture.detectChanges();
}));
// beforeEach(() => {
// fixture = TestBed.createComponent(WatchpanelComponent);
// component = fixture.componentInstance;
// fixture.detectChanges();
// });
it('should create', () => {
expect(component).toBeTruthy();
});
});
據我了解,@angular/material 現在包含唯一需要導入的模塊 MaterialModule。 我嘗試從 @angular2-material/icon 導入 MdIconModule,但沒有成功。 我究竟做錯了什么 ? 提前致謝
按照 yurzui 的建議導入 MaterialModule 並在返回承諾后創建組件解決了它。 謝謝yurzui
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { DebugElement } from '@angular/core';
import { WatchpanelComponent } from './watchpanel.component';
import { FormsModule } from '@angular/forms';
import { MaterialModule } from '@angular/material';
describe('WatchpanelComponent', () => {
let component: WatchpanelComponent;
let fixture: ComponentFixture<WatchpanelComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [ MaterialModule.forRoot() ],
// forRoot() deprecated
// in later versions ------^
declarations: [ WatchpanelComponent ] // declare the test component
})
.compileComponents().then(() => {
fixture = TestBed.createComponent(WatchpanelComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
}));
// beforeEach(() => {
// fixture = TestBed.createComponent(WatchpanelComponent);
// component = fixture.componentInstance;
// fixture.detectChanges();
// });
it('should create', () => {
expect(component).toBeTruthy();
});
});
自@javahaxxor 的回答以來,較新版本的 Angular Material 發生了變化。 我已經通過導入與AppModule
相同的模塊解決了這個問題(我在這里也需要表單):
import {
MatButtonModule,
MatCardModule,
MatIconModule,
MatInputModule,
MatProgressSpinnerModule,
MatDialogModule,
MatCheckboxModule
} from '@angular/material';
// ... not important
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ WelcomeComponent ],
imports: [
NoopAnimationsModule,
FormsModule,
ReactiveFormsModule,
MatButtonModule,
MatCardModule,
MatIconModule,
MatInputModule,
MatProgressSpinnerModule,
MatDialogModule,
MatCheckboxModule
],
providers: [
// ...
]
})
.compileComponents();
}));
md-icon在最新版本的 Angular Material 中不再可用。 所有標簽/元素現在都以“mat”而不是“md”為前綴..所以<md-icon>
..becomes.. <mat-icon>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.