使用Vue.extend(require('./message.vue'));导入一个消息组件放到body里面,不知道什么原因,一直导入的只有el的空div,message.vue里面的template没有导入进来,求教是什么问题?
src/components/toast/index.js
import Vue from 'vue';
let MyMsgConstructor = Vue.extend(require('./message.vue'));
let instance;
let message = function (msg) {
instance = new MyMsgConstructor({
el: document.createElement("div"),
data: {
message: msg,
visible: true
}
});
document.body.appendChild(instance.$el);
return instance;
};
export default message;
src/components/toast/message.vue
<template>
<transition name="el-message-fade">
<div v-show="visible" class="my-msg">{{message}}</div>
</transition>
</template>
<script>
export default {
data() {
return {
message: '',
visible: true
}
},
methods: {
close() {
setTimeout(() => {
this.visible = false;
}, 2000)
},
},
mounted() {
this.close();
}
}
</script>
main.js
import message from './components/toast/index.js'
Vue.$message = Vue.prototype.$message = message;
使用
openToast() {
this.$message("test");
}