angular material snackbar undefined after project build

16 views Asked by At

I have an issue with angular material snackbar. It works on localhost, but after build process I got an error:

Cannot read properties of undefined (reading 'openSnackBar')

I call a commonservce that provides snackbar func, from other service file.

call:

private async handle() {
    this.commonService.openSnackBar('test snackbar');
...

commonService:

 public openSnackBar(message: string, icon?: string) {
    return this.zone.run(() => {
      // this.snackbar.open(message, 'x', config);
      this._snackBar.open(message, icon, { horizontalPosition: this.horizontalPosition, verticalPosition: this.verticalPosition, duration: 3000 });
    });
  }

ps zoge was added, but nothing changed. still works on localhost but not after build process.

1

There are 1 answers

0
Uland Nimblehoof On

I resolved the issue. Method call was executed from constructor. Solution was inject dependant service to calling method inside constructor as method param and remove "this" from call method body.

constructor(private commonService: CommonService) {
  setTimeout(async () => {
    await this.handle(commonService);
    ...

execute:

private async handle(commonService: CommonService) {
    commonService.openSnackBar('test snackbar');
...