角材料和茉莉花:没有提供InjectionToken MdDialogData的提供程序! " [英] Angular Material and Jasmine : " No provider for InjectionToken MdDialogData! "

查看:166
本文介绍了角材料和茉莉花:没有提供InjectionToken MdDialogData的提供程序! "的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要在Angular Material MdDialog中使用的组件:

I have a component which is meant to be used in an Angular Material MdDialog :

@Component({
  ...
})
export class MyComponent {

  constructor(@Inject(MD_DIALOG_DATA) public data: any, public dialogRef: 
MdDialogRef<MyComponent>) {
...
  }


}

我正在尝试使用Jasmine进行单元测试:

I am trying to Unit Test it with Jasmine :

describe('MyComponent', () => {
  let component: MyComponent;
  let fixture: ComponentFixture<MyComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [
        SharedTestingModule,
      ],
      declarations: [
        MyComponent,
      ],
    })
    .compileComponents();
  }));

  ...

});

不幸的是,我收到以下错误消息:

Unfortunately, I am getting the following error :

错误:没有提供InjectionToken MdDialogData的提供程序!

Error: No provider for InjectionToken MdDialogData!

SharedTestingModule导入和导出我的自定义Angular Material模块,该模块本身导入和导出MdDialogModule.

SharedTestingModule imports and exports my custom Angular Material module, which itself imports and exports MdDialogModule.

如何摆脱这个错误?

非常感谢!

Angular 4.2.4
Angular Material 2.0.0-beta.7
Jasmine 2.5.3

推荐答案

我添加了此内容:

providers: [
    { provide: MD_DIALOG_DATA, useValue: {} },
    { provide: MdDialogRef, useValue: {} }
]

它有效:)

感谢您的@methgaard帮助!

Thanks for your help @methgaard!

这篇关于角材料和茉莉花:没有提供InjectionToken MdDialogData的提供程序! &quot;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆