gpt4 book ai didi

angular - 我无法注入(inject) MAT_DIALOG_DATA!错误 : inject() must be called from an injection context

转载 作者:行者123 更新时间:2023-12-01 21:58:02 25 4
gpt4 key购买 nike

我正在使用 MAT_DIALOG_DATA 将数据传递到我的对话框,但是,我在我的浏览器控制台中收到此错误。

Uncaught Error: inject() must be called from an injection context
at injectInjectorOnly (core.js:1767)
at inject (core.js:1778)
...
...
...

首先,我必须指出我使用的是 angular 7。

我观看了很多视频并阅读了很多文章,这些文章表明我在我的项目中包含的内容就足够了:- 我从 Angular Material 中导入了 MAT_DIALOG_DATA。- 我使用了对话框引用。- 我在构造函数中定义我的注入(inject)。

这里是 component.ts 代码:

import { Component, OnInit, inject } from '@angular/core';
import { MatDialogRef, MAT_DIALOG_DATA} from '@angular/material';

@Component({
selector: 'app-table-editor-dialog',
templateUrl: './table-editor-dialog.component.html',
styleUrls: ['./table-editor-dialog.component.scss']
})
export class TableEditorDialogComponent implements OnInit {

constructor(
@inject(MAT_DIALOG_DATA) public data: any,
public matDialogRef: MatDialogRef<TableEditorDialogComponent>
) { }

ngOnInit() {
}

getMessageId(id : Number){
}
}

我希望将数据传递给对话框。现在是怎么回事。我的程序编译成功,但在浏览器中,程序停止工作并显示错误中的错误。

Uncaught Error: inject() must be called from an injection context
at injectInjectorOnly (core.js:1767)

最佳答案

导入应该是Inject,而不是inject。因此,使用以下导入

import { Component, OnInit, Inject } from '@angular/core';

并且,在组件的构造函数中

constructor(public dialogRef: MatDialogRef<TableEditorDialogComponent>,
@Inject(MAT_DIALOG_DATA) public data: any) { }

Correct Injection of MAT_DIALOG_DATA

关于angular - 我无法注入(inject) MAT_DIALOG_DATA!错误 : inject() must be called from an injection context,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55329999/

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