您的解决方案是创建一个snackbar 组件。
没有看到你的代码,我猜你大致是这样的:
this.snackBar.open(this.sampleStringErrorMsg, null, {
verticalPosition: 'top'
});
据我所知,没有办法通过像上面那样做来实现你想要的。
- 创建一个组件。我会叫它
ErrorSnackBarComponent。
在html 上添加错误消息的内容。它看起来像这样:
<div>
<p>
<span>The sample is not valid:</span>
<br/>
<span>The key and key value are required in the sample.</span>
<br/>
<span>The delimiter must be entered when the sample contains several key-value pairs.</span>
<br/>
<span>The delimiter must not be equal to the key or key value.</span>
</p>
</div>
-
确保ErrorSnackBarComponent 在app.module.ts 下被引用:
使用您最近创建的snackbar 组件:
this.snackBar.openFromComponent(ErrorSnackBarComponent, {
verticalPosition: 'top'
});
额外步骤
如果您想将数据传递给ErrorSnackBarComponent,请将您的快餐栏组件的构造函数更改为:
constructor(@Inject(MAT_SNACK_BAR_DATA) public data: any) { }
而不是 3.,使用这个:
this.snackBar.openFromComponent(ErrorSnackBarComponent, {
verticalPosition: 'top',
data: <YOUR_DATA_HERE>
});
您应该能够使用ErrorSnackBarComponent 中的data 并将错误消息与任何其他详细信息(例如属性)一起显示。
Here is the documentationsnackbar 供您参考。