嘿,我是Angular的新手,我想从matsnackbar获取数据。
有可能吗?
apiName: string;
this.snackBar.openFromComponent(CustomSnackbarComponent, {
duration: 5000000,
data: this.apiName;
});
我的组件:
export class CustomSnackBarComponent implements OnInit {
constructor(private sanitizer: DomSanitizer) { }
ngOnInit() {
}
}
答案 0 :(得分:7)
您必须在快餐栏组件中注入MAT_SNACK_BAR_DATA
:
import {Component, Inject} from '@angular/core';
import {MAT_SNACK_BAR_DATA} from '@angular/material';
@Component({
selector: 'your-snack-bar',
template: '{{ data }}',
})
export class CustomSnackBarComponent {
constructor(@Inject(MAT_SNACK_BAR_DATA) public data: any) { }
}