You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为程序化触发的UIkit 3 Notification绑定Close事件?

UIkit Notification Close Event: Works for Programmatically Created Instances

Absolutely! The close event for UIkit Notification does work with programmatically created instances—including the persistent one you’ve set up with timeout: null (which stays open until manually dismissed or closed via code).

Here’s how to make it work properly with your existing setup:

  1. Capture the notification instance (as you already started doing):

    const notification = UIkit.notification({ 
      message: 'my-message!', 
      status: 'primary', 
      timeout: null 
    });
    
  2. Bind the close event to the instance using UIkit’s built-in event handling:

    notification.on('close', () => {
      console.log('Notification has been closed!');
      // Add your custom logic here (e.g., update UI, trigger another action)
    });
    

This event will fire in all scenarios where the notification is closed:

  • When the user clicks the notification’s built-in close button
  • When you call notification.close() programmatically (e.g., from a button click or other trigger)
  • Even if you had set a timeout value (the event fires when the auto-dismissal completes)

For example, if you wanted to trigger the close action via a button:

// Add a button in your HTML: <button id="close-notif">Close Notification</button>
document.querySelector('#close-notif').addEventListener('click', () => {
  notification.close();
  // The close event handler above will run automatically after this
});

The key point is that the returned instance from UIkit.notification() gives you full access to all the component’s events and methods, including close.

内容的提问来源于stack exchange,提问作者frumbert

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:48:40