Angular 如何知道单击了哪个特定toastr?
我已经成功地展示了ngx烤面包机制作的土司。 由我展示了烤面包机的各种类型的通知,根据id和任务代码 我试图得到点击回调和动作回调,但没有找到任何回报Angular 如何知道单击了哪个特定toastr?,angular,toastr,Angular,Toastr,我已经成功地展示了ngx烤面包机制作的土司。 由我展示了烤面包机的各种类型的通知,根据id和任务代码 我试图得到点击回调和动作回调,但没有找到任何回报 onNotificationReceived(res) { let key; switch (key) { case 1: showSuccessPopup(res.title,res.body);
onNotificationReceived(res) {
let key;
switch (key) {
case 1:
showSuccessPopup(res.title,res.body);
break;
default:
break;
}
}
showSuccessPopup(title,body) {
this.toastr
.success(title, body, { closeButton: true })
.onTap.subscribe((action) => console.log(action))
}
实际结果:如图所示
应为:显示id toast的此.toastr.success()
显示toastr通知并返回一个ActiveToast
-对象。您可以在特定的toastr上调用onTap
。如果要访问订阅中的toastr对象,请将其保存在变量中:
const toastr = this.toastr.success(/* ... */);
toastr.onTap.subscribe(action => /* use toastr here */);
/* you can also use toastr here */
toastr
如下所示:
export interface ActiveToast {
/** Your Toast ID. Use this to close it individually */
toastId: number;
/** the message of your toast. Stored to prevent duplicates */
message: string;
/** a reference to the component see portal.ts */
portal: ComponentRef<any>;
/** a reference to your toast */
toastRef: ToastRef<any>;
/** triggered when toast is active */
onShown: Observable<any>;
/** triggered when toast is destroyed */
onHidden: Observable<any>;
/** triggered on toast click */
onTap: Observable<any>;
/** available for your use in custom toast */
onAction: Observable<any>;
}
导出接口ActiveToast{
/**您的Toast ID。使用此项单独关闭它*/
蟾蜍:数字;
/**您的祝酒词。存储以防止重复*/
消息:字符串;
/**组件的引用请参见portal.ts*/
门户:ComponentRef
您可以将toastId
从实际的ActiveToast实例传递到回调
不幸的是,所有回调都不会返回实际的toast对象本身!如果一次收到多个通知会怎么样?较新的通知将更改当前值。。。
onNotificationReceived(res) {
let key;
switch (key) {
case 1:
showSuccessPopup(res.title,res.body);
break;
default:
break;
}
}
showSuccessPopup(title,body) {
const toast = this.toastr
.success(title, body, { closeButton: true });
toast.onTap.subscribe((action) => console.log(toast.toastId));
}