gpt4 book ai didi

angular - NullInjectorError : No provider for InjectionToken MatDialogData

转载 作者:行者123 更新时间:2023-12-03 14:34:27 26 4
gpt4 key购买 nike

在我的 Angular 应用程序上运行 jasmine 测试时出现此错误。

Error: StaticInjectorError(DynamicTestModule)[MyEditDialogComponent -> InjectionToken MatDialogData]: 
StaticInjectorError(Platform: core)[MyEditDialogComponent -> InjectionToken MatDialogData]:
NullInjectorError: No provider for InjectionToken MatDialogData!
error properties: Object({ ngTempTokenPath: null, ngTokenPath: [ 'MyEditDialogComponent', InjectionToken MatDialogData ], ngDebugContext: DebugContext_({ view: Object({ def: Object({ factory: Function, nodeFlags: 33669121, rootNodeFlags: 33554433, nodeMatchedQueries: 0, flags: 0, nodes: [ Object({ nodeIndex: 0, parent: null, renderParent: null, bindingIndex: 0, outputIndex: 0, checkIndex: 0, flags: 33554433, childFlags: 114688, directChildFlags: 114688, childMatchedQueries: 0, matchedQueries: Object({ }), matchedQueryIds: 0, references: Object({ }), ngContentIndex: null, childCount: 1, bindings: [ ], bindingFlags: 0, outputs: [ ], element: Object({ ns: '', name: 'app-edit-ban-dialog', attrs: [ ], template: null, componentProvider: Object({ nodeIndex: 1, parent: <circular reference: Object>, renderParent: <circular reference: Object>, bindingIndex: 0, outputIndex: 0, checkIndex: 1, flags: 114688, childFlags: 0, directChildFlags: 0, childMatchedQueries: 0, matchedQueries: Object, matchedQueryIds: 0 ...
Error: StaticInjectorError(DynamicTestModule)[MyEditDialogComponent -> InjectionToken MatDialogData]:
StaticInjectorError(Platform: core)[MyEditDialogComponent -> InjectionToken MatDialogData]:
NullInjectorError: No provider for InjectionToken MatDialogData!

我在我的组件中导入:
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material';

在组件构造函数中,我将数据注入(inject)对话框:
 @Inject(MAT_DIALOG_DATA) public data: {
myData : MyData
}

在我要导入的组件的 karma jasmin 规范中:
import {
MatFormFieldModule,
MatInputModule,
MatDialogModule,
MatDialogRef,
MAT_DIALOG_DATA,
MatButtonModule,
MatRadioModule,
MatSelectModule
} from '@angular/material';

beforeEach 中的组件、导入和提供程序的 karma/jasmine 规范中:
 beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ MyFormComponent ],
imports: [
MatFormFieldModule,
MatInputModule,
MatDialogModule,
],
providers: [
{ provide: MatDialogRef, useValue: {} },
{ provide: MAT_DIALOG_DATA, useValue: { myData: MyData } }
]
}).compileComponents();
}));

我已经尝试了这些答案,但它们没有解决问题
This
This

最佳答案

就我而言,我导入了指令 MatDialogModule、MAT_DIALOG_DATA 和 MatDialogRef

import { MatDialogModule, MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
然后在 beforeEatch() 中:
imports: [ MatDialogModule],
providers: [
{ provide: MAT_DIALOG_DATA, useValue: {} },
{ provide: MatDialogRef, useValue: {} }
]

关于angular - NullInjectorError : No provider for InjectionToken MatDialogData,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57996566/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com