项目构建后未定义角度材质小吃栏

问题描述 投票:0回答:1

我对有角度的材料小吃栏有疑问。它可以在本地主机上运行,但是在构建过程之后我收到一个错误:

Cannot read properties of undefined (reading 'openSnackBar')

我从其他服务文件中调用提供小吃栏功能的公共服务。

致电:

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

通用服务:

 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,但没有任何改变。仍然可以在本地主机上运行,但在构建过程之后就不行了。

angular typescript angular-material snackbar
1个回答
0
投票

我解决了这个问题。方法调用是从构造函数执行的。解决方案是将依赖服务注入构造函数内的调用方法作为方法

param
,并从调用方法主体中删除“
this
”。

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

执行:

private async handle(commonService: CommonService) {
    commonService.openSnackBar('test snackbar');
...
© www.soinside.com 2019 - 2024. All rights reserved.