跨组件通知vue插件
English | 简体中文
What's Vue Notification CenterA lightweight vue cross-component communication plugin,designed with Subscribe/Publish mode。when Publisher post notifications with specific topic,subscribers will receive the notification if they subscribe this topic。
Note that, Notification Center is a global singleton
Downloadusing npm
$ npm i vue-notification-center -S Install
in main.js
:
import Vue from 'vue' import nc from 'vue-notification-center' Vue.use(nc) How to use Publisher:
// componentA.vue methods: { someMethod () { let data = {msg: 'data to be transmitted'} this.$nc.publish('someTopic', data) } }
fine, you just published a notification with topic someTopic
,subscribers will receive the notification if they subscribe topic named 'someTopic'。
the Notification Center will not broadcast the notification if this topic has no subscribers, it will silently fail.
Subscriber:// componentB.vue methods: { onReceived (message) { let {notifyFrom, notifyTo, data} = message console.log(notifyFrom, notifyTo, data) } }, created () { this.$nc.subscribe('someTopic', this.onReceived) }
componentB
subscribed to the notification with topic someTopic
.if someone published this notification, and then Notification Center will invoke the given callback
Before the subscriber component instance is destroyed, it will automatically unsubscribes all subscribed topics without having to manually cancel. Subsequent versions will be added with a manual unsubscribe method.
APIthe api has been mixin to all vue instance,you can call them like this.$XXXX
name | parameter | description |
---|---|---|
$nc | Function() | return current topics in Notification Center |
$nc.subscribe | Function(topic:String, callback:Function) | topic: topic name to subscribe. |
$nc.publish | Function(topic:String, data:Object):() | topic: topic name to publish.data: carrying data |
版权声明:
1、该文章(资料)来源于互联网公开信息,我方只是对该内容做点评,所分享的下载地址为原作者公开地址。2、网站不提供资料下载,如需下载请到原作者页面进行下载。