angular - NullInjectorError: Nema pružatelja za MatDialogRef
Ne mogu ubrizgati MatDialogRef kako je opisano u dokumentaciji: https://material.angular.io/components/dialog/overview
Kad pokušavam to napraviti, pojavila se pogreška:
GRESKA Pogreška: StaticInjectorError[MatDialogRef]: StaticInjectorError[MatDialogRef]: NullInjectorError: Nema pružatelja za MatDialogRef!
app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import {
MatInputModule,
MatDialogModule,
MatProgressSpinnerModule,
MatButtonModule,
MatDialog,
MatDialogRef
} from '@angular/material';
import { ApiModule } from '../api/api.module';
import { RoutingModule } from '../routing/routing.module';
import { RegistrationComponent } from './components/registration.component';
import { LoginComponent } from './components/login.component';
import { AccountService } from './services/account.service';
@NgModule({
imports: [
BrowserModule,
MatInputModule,
MatDialogModule,
MatProgressSpinnerModule,
MatButtonModule,
FormsModule,
RoutingModule,
ApiModule
],
declarations: [
RegistrationComponent,
LoginComponent
],
entryComponents: [
LoginComponent,
RegistrationComponent
],
providers: [
AccountService,
MatDialog,
MatDialogRef
]
})
export class AccountModule {}home.component.ts
import { Component } from '@angular/core';
import { MatDialog } from '@angular/material';
import { RegistrationComponent } from '../account/components/registration.component';
@Component({
moduleId: module.id.replace('compiled', 'app'),
templateUrl: 'home.component.html'
})
export class HomeComponent
{
constructor(private modalService: MatDialog) {}
public openModal() : void
{
let dialog = this.modalService.open(RegistrationComponent, {});
}
}registracija.komponenta.ts
import { Component } from '@angular/core';
import { Router } from '@angular/router';
import { MatDialogRef } from '@angular/material/dialog';
import { User } from '../../../models/domain/User';
import { ApiUserService } from '../../api/entity-services/user.service';
import { AuthService } from '../../auth/auth.service';
import { AccountService } from '../services/account.service'
@Component({
selector: 'registration-component',
templateUrl: 'app/modules/account/templates/registration.component.html'
})
export class RegistrationComponent
{
public user :User = new User();
public errorMessage :string;
public isLoading :boolean;
constructor
(
private userService :ApiUserService,
private authService :AuthService,
private accountService :AccountService,
private router :Router,
public dialogRef :MatDialogRef<RegistrationComponent>
)
{
this.isLoading = false;
}
public onSubmit(e) :void
{
e.preventDefault();
this.isLoading = true;
this.userService
.Create(this.user)
.subscribe(
user =>
{
this.user.id = user.id;
this.user.login = user.login;
this.authService
.Login(this.user)
.subscribe(
token =>
{
this.accountService.Load()
.subscribe(
account =>
{
this.user = account;
this.isLoading = false;
this.dialogRef.close();
let redirectRoute = account.activeScopeId
? `/scope/${account.activeScopeId}`
: '/scope-list/';
this.router.navigate([redirectRoute]);
},
error => this.errorMessage = <any>error
);
},
error => this.errorMessage = <any>error
);
},
error => this.errorMessage = <any>error
);
}
}Odgovor #1
Imao sam ovu pogrešku prilikom dodavanja dijaloga u servis koji će se dijeliti među mnogim komponentama. Samo da pojasnim, pogreška nije bila prisutna u aplikaciji prije nego što su dijalozi premješteni u servis. Rješenje je bilo uključiti prilagođenog providera MatDialogRef u glavni modul
import { DialogService } from './services/dialog.service';
import { MatDialogModule, MatDialogRef } from '@angular/material/dialog';
...
imports: [
...
MatDialogModule
],
providers: [
{
provide: MatDialogRef,
useValue: {}
},
DialogService
],
...
S ovim pružateljem usluga, servis je radio kao singleton s mojim dijalozima koje je trebalo dijeliti i pogreška pružatelja usluga je nestala.
Odgovor #2
Zahvaljujući @Edric, riješio sam problem uvozenjem MatDialogModule, MatDialog i MatDialogRef iz @angular/material/dialog umjesto iz @angular/material
Odgovor #3
Na primjer, koristite app-checkout
To se može dogoditi ako koristite komponentu na oba načina, 'dialog' i 'normalan', dodajući app-checkout u normalnu HTML datoteku.
Rješenje 1: uklonite <app-checkout> </app-checkout> ako ga ne trebate uvesti na normalan način u HTML datoteku
Rješenje 2 koje se koristi i u dijalogu i u HTML datoteci. Umjesto ovoga:
// ... something have selector: "app-checkout",
constructor(
public dialogRef: MatDialogRef<CheckoutComponent>,
@Inject(MAT_DIALOG_DATA) public dialogData: any
) {}
trebate ga ubrizgati (da biste ga učinili opcionalnim, ubrizgajte):
na ovaj način:
constructor(
// only need the @Optional() before the public dialogRef
@Optional() public dialogRef: MatDialogRef<CheckoutComponent>,
@Inject(MAT_DIALOG_DATA) public dialogData: any
) {
}
ili ovako:
// ... the same above
private dialogRef = null;
private dialogData;
constructor(private injector: Injector) {
this.dialogRef = this.injector.get(MatDialogRef, null);
this.dialogData = this.injector.get(MAT_DIALOG_DATA, null);
}
Odgovor #4
Samo dodajte @Optional() prije vaše deklaracije dialogRef.
Na primjer:
constructor(
@Optional() public dialogRef: MatDialogRef<yourDialogComponentName>
) {
}
