加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angular – 如何从MatSnackBarConfig获取数据属性

发布时间:2020-12-17 17:52:08 所属栏目:安全 来源:网络整理
导读:我在 documentation中读过,MatSnackBarConfig对象可以有一个表示的数据属性 Data being injected into the child component. 所以我想我可以在通过openFromComponent方法打开的自定义零食栏中使用这些数据.问题是我可以这样做吗?如果是,那我该怎么做? 在文
我在 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) { }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读