【问题标题】:How to know which specific toastr was clicked?如何知道点击了哪个特定的 toastr?
【发布时间】:2019-04-18 08:36:08
【问题描述】:

我已经通过 ngx-toaster 成功展示了 toast。 通过我根据 id 和 taskcode 显示各种类型的通知的烤面包机。

我尝试获取点击回调和操作回调,但没有找到任何回报。

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

【问题讨论】:

    标签: angular toastr


    【解决方案1】:
    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));
        }
    

    您可以将toastId 从实际的 ActiveToast 实例传递给您的回调。

    不幸的是,没有一个回调将返回实际的 toast 对象本身!

    【讨论】:

      【解决方案2】:

      this.toastr.success() 显示一个 toastr 通知并返回一个 ActiveToast-Object。你在那个特定的烤面包机上打电话给onTap。如果您想访问订阅中的 toastr-object,请将其保存在变量中:

      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>;
      }
      

      查看文档:https://www.npmjs.com/package/ngx-toastr

      【讨论】:

      • 如果一次收到多个通知怎么办?较新的通知将更改当前值...
      猜你喜欢
      • 2012-04-18
      • 1970-01-01
      • 2012-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多