看了element ui的源码想学习其思路做一个自定义的动态组件,具体操作如下:
创建一个main.vue文件:

再创建一个main.js
import Vue from 'vue';
let AlertConstructor = Vue.extend(require('./main.vue'));
let instance;
var Alert= function (msg) {
instance = new AlertConstructor({
data: {
message: msg
}
});
instance.$mount();
document.body.appendChild(instance.$el);
return instance;
};
export default Alert;
在主入口main.js引入组件:
import Alertfrom './alert/src/main.js';
Vue.prototype.$myAlert = Alert;
然后在页面上测试一下:
<button @click='test'>按钮</button>
methods:{
test(){
this.$myAlert("hello vue");
}
}
此时控制台报错:
[Vue warn]: Failed to mount component: template or render function not defined.
百思不得其解???
 PIPIONE
PIPIONE 
					温温酱
 随时随地看视频慕课网APP
随时随地看视频慕课网APP
相关分类