【发布时间】:2020-01-22 17:57:04
【问题描述】:
我从教程中制作了一个基本的模式对话框,有点卡住了,加载我的项目给我带来了这个错误:
ERROR Error: Uncaught (in promise): Error: inject() must be called from an injection context
Error: inject() must be called from an injection context
at injectInjectorOnly (core.js:728)
我不确定为什么错误消息显示小写-i injection() 而不是大写-I @Inject,我在构造函数中调用它
import { Component, NgModule, OnInit } from '@angular/core';
import { Inject } from '@angular/core';
import { MatDialog, MatDialogRef, MatDialogConfig, MatDialogModule, MAT_DIALOG_DATA } from '@angular/material/dialog';
@Component({
selector: 'term-component',
templateUrl: './term.component.html'
})
@NgModule({
imports: [MatDialog, MatDialogRef, MatDialogConfig, MatDialogModule],
providers: [{
provide: MatDialog, useValue: {}
}]
})
export class TermDialog implements OnInit {
constructor(
@Inject(MAT_DIALOG_DATA) private modalData: any,
public dialogRef: MatDialogRef<TermDialog>
) {
console.log(this.modalData);
}
ngOnInit() { alert(''); }
actionFunction() {
this.closeModal();
}
closeModal() {
this.dialogRef.close();
}
onNoClick(): void {
this.dialogRef.close();
}
}
所有类似的帮助主题都说要确保我使用的是大写字母-I @Inject 而不是我正在做的注入。但是,我确实看到了从“@angular/core/testing”而不是“@angular/core”导入 Inject 的建议,我认为这是不可能的?
任何帮助都将不胜感激,我以前从来没有在 Hello World 项目上被卡住这么多次。
新:
好的,我认为我已经满足了所有 5 个要求,但我仍然看到同样的错误。 这是我的 app.module.ts:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
import { RouterModule } from '@angular/router';
import { OverlayModule } from '@angular/cdk/overlay';
import { MatDialogModule } from '@angular/material/dialog';
import { AmplifyAngularModule, AmplifyService } from 'aws-amplify-angular';
import { AppComponent } from './app.component';
import { NavMenuComponent } from './nav-menu/nav-menu.component';
import { HomeComponent } from './home/home.component';
import { CounterComponent } from './counter/counter.component';
import { FetchDataComponent } from './fetch-data/fetch-data.component';
import { TermComponent } from './term.component';
import { TermDialog } from './term-dialog.component';
import { LoginComponent } from './login/login.component';
@NgModule({
declarations: [
AppComponent,
NavMenuComponent,
HomeComponent,
CounterComponent,
FetchDataComponent,
TermComponent,
LoginComponent,
TermDialog
],
imports: [
AmplifyAngularModule,
BrowserModule.withServerTransition({ appId: 'ng-cli-universal' }),
HttpClientModule,
FormsModule,
OverlayModule,
MatDialogModule,
RouterModule.forRoot([
{ path: '', redirectTo: '/login', pathMatch: 'full' },
{ path: 'login', component: LoginComponent },
{ path: 'counter', component: CounterComponent },
{ path: 'fetch-data', component: FetchDataComponent },
{ path: 'term', component: TermComponent },
{ path: '**', component: LoginComponent }
])
],
entryComponents: [
TermDialog
],
providers: [
AmplifyService
],
bootstrap: [AppComponent]
})
export class AppModule { }
我的 term.component 文件应该启动术语对话框
import { Component, NgModule } from '@angular/core';
import { MatDialog, MatDialogConfig, MatDialogModule, MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
import { TermService } from '../../services/term.service';
import { TermDialog } from './term-dialog.component';
import { compileNgModule } from '@angular/compiler';
@Component({
selector: 'term-component',
templateUrl: './term.component.html'
})
@NgModule({
imports: [MatDialogModule],
providers: [{ provide: MatDialog, useValue: {}
}]
})
export class TermComponent {
clicked: boolean;
success: boolean;
constructor(public dialog: MatDialog, private termService: TermService) {
this.clicked = false;
}
openAddNewTermDialog() {
const dialogConfig = new MatDialogConfig();
dialogConfig.disableClose = true;
dialogConfig.height = "350px";
dialogConfig.width = "600px";
dialogConfig.data = {
};
const dialogRef = this.dialog.open(TermDialog, dialogConfig);
dialogRef.afterClosed().subscribe(result => {
console.log('closed dialog');
this.success = result;
})
}
}
【问题讨论】:
-
尝试从两个组件文件中删除 NgModule 并且不要忘记删除与其相关的所有导入(只是为了节省一些空间 - 虽然不是关键)
-
我看到您没有传递任何数据,您也可以在 TermDialog 中删除声明 Inject 。这不是强制性的。
-
所以这些只是表面上的改变,对吧?如何解决“从注入上下文注入”问题?
-
我认为它与 TermDialog 组件中的选择器和 templateURL 有关系。让我确认需要进行哪些更改。
标签: angularjs angular-material