vue mount失败

import Vue from 'vue';
export default ()=>{
    Vue.prototype.$alert= (options)=>{
        return new Promise((resolve, reject)=>{
            let alert = Vue.extend(require('./alert.vue'));

            options.callback=resolve
            let instance=new alert({
                data: options
            });
            
            instance.$mount();
            console.log(instance)
            document.body.appendChild(instance.$el);
        })
    }
}

在其他vue组件调用 this.$alert() 时失败
Failed to mount component: template or render function not defined
vue-cli构建。。。

是instance.$mount() 挂载失败。。this.$alert()可以调用

生成的东西是这个<!--function (a, b, c, d) { return createElement(vm, a, b, c, d, true); }-->

阅读 4.3k
2 个回答

export default {

install: (Vue) => {
    Vue.prototype.$alert.......
}

}

我记得Vue的插件需要有个install方法吧

直接去根组件所在的地方写的。

Vue.prototype.$alert= (options)=>{
        return new Promise((resolve, reject)=>{
        })
    }

就行了 不用导出。
或者

export default (options)=>{
        return new Promise((resolve, reject)=>{
        ...
        })
    }

在别的地方
import $alert from '...'
Vue.Vue.prototype.$alert = $alert

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题