假设我有一个简单的Vue插件:
插件.js
export default {
install(Vue) {
if (this.installed) {
return;
}
this.installed = true;
Vue.prototype.$myPlugin = {
newEvent: () => {
this.$emit('new-event-from-plugin'); // <-- does not work :(
}
};
}
}
我想在我的主Vue应用程序上使用它:
App.vue
<template>
<div>My app</div>
</template>
<script>
export default {
name: 'app',
created() {
this.$on('new-event-from-plugin', () => {
console.log('is it working?');
});
},
mounted() {
this.$myPlugin.newEvent();
}
}
</script>
假设我已正确注册插件,如何从插件发出事件并在主应用程序上收听?
我也尝试过使用:Vue.prototype.$emit(); Vue.$root.$emit();
幕布斯6054654
Smart猫小萌
相关分类