我在
documentation中读过,MatSnackBarConfig对象可以有一个表示的数据属性
Data being injected into the child component.
所以我想我可以在通过openFromComponent方法打开的自定义零食栏中使用这些数据.问题是我可以这样做吗?如果是,那我该怎么做?
在文档中提供了an example,假设我将openSnackBar方法修改为以下内容:
应用程序/小吃店组分-example.ts
openSnackBar() { this.snackBar.openFromComponent(PizzaPartyComponent,{ duration: 500,data: {message: 'Hello World!'} }); }
现在,我如何在PizzaPartyComponent中获取数据对象?
我试图将它注入组件的构造函数,但无法解决.
//below code results in error export class PizzaPartyComponent { constructor(private data: any) { console.log(data); } }
解决方法
我在材料
github page上找到了解决方案.
事实证明我以错误的方式注入了数据.合适的是:
import {MAT_SNACK_BAR_DATA} from '@angular/material'; // @Component decorator omitted export class PizzaPartyComponent { constructor(@Inject(MAT_SNACK_BAR_DATA) public data: any) { } }