我使用angular-cli创建我的组件并使用Material2。
在test.component.html
中<md-toolbar>
<button md-icon-button [mdMenuTriggerFor]="anchorMenu" aria-label="Open anchor menu">
<md-icon>more_vert</md-icon>
</button>
</md-toolbar>
<md-menu #anchorMenu="mdMenu"> //getting error on this line
<a md-menu-item *ngFor="let item of items" href="http://www.google.com" [disabled]="item.disabled">
{{ item.text }}
</a>
</md-menu>
单元测试时:
错误:./TestCompComponent类中的错误TestCompComponent - 内联 模板:7:4引起:无法读取属性&#39;取消订阅&#39;的 未定义
当我运行 ng serve 时,Everyhing工作正常,但在 ng test
上面出现错误在test.component.spec.ts
中import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import 'hammerjs';
import { MaterialModule } from '@angular/material';
import { TestComponent } from './test.component';
describe('TestComponent', () => {
let component: TestComponent;
let fixture: ComponentFixture<TestComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [MaterialModule],
declarations: [TestComponent]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(TestComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
一些答案说&#34; fixture.detectChanges();&#34;会工作,但这已经存在,不适合我。更多的答案说需要添加Observable,但我没有使用任何服务。这是Material2的简单MdMenu。
调试:
请找到所有相关文件参考的plnkr链接。 (请注意,这不适用于plnkr,仅供参考。)