2 回答

TA貢獻1876條經驗 獲得超7個贊
Vue.prototype.$myPlugin = function () {
return {
newEvent: function () {
this.$emit('new-event-from-plugin');
}.bind(this)
};
然后像這樣使用它
this.$myPlugin().newEvent()

TA貢獻1911條經驗 獲得超7個贊
在Vuejs中,當我們想要發出帶有事件的特定值并在父組件中偵聽事件時,我們使用該事件。實際上,我們在 vue 組件中玩,你想從插件中發出一個事件.js!$emit()$emit()
我建議你使用 pubsub.js。這是有用和驚人的庫,為您的自定義偵聽器。
你可以像這樣使用它:
import PubSub from 'pubsub-js';
export default {
install(Vue) {
if (this.installed) {
return;
}
this.installed = true;
PubSub.publish('new-event-from-plugin');
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<template>
<div>My app</div>
</template>
<script>
import PubSub from 'pubsub-js';
export default {
name: 'app',
created() {
PubSub.subscribe('new-event-from-plugin', () => {
console.log("it's worked!");
});
}
}
</script>
添加回答
舉報