Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.1k views
in Technique[技术] by (71.8m points)

Vue3如何在挂载到全局的插件中使用第三方UI库

需求是一个全局的登录弹窗
在vue2我是extend一个子类,实例化后挂在全局,并把显示弹窗的方法挂在在原型上。
vue2这样写

// plugins/LoginDialog.js
import LoginDialogCom from "@/components/LoginDialog.vue";

export default {
  install: function (Vue) {
    const LoginDialogConstructor = Vue.extend(LoginDialogCom);
    const instance = new LoginDialogConstructor();

    instance.$mount(document.createElement("div"));
    document.body.appendChild(instance.$el);

    Vue.prototype.$loginDialog = () => {
      instance.loginVisible = true;
    };
  },
}; 

vue3没有extend,我这样写

// plugins/LoginDialog.js
import LoginDialogCom from '@/components/LoginDialog.vue';

import { createApp } from 'vue';

export default {
  install: (app) => {
    const instance = createApp(LoginDialogCom).mount(
      document.createElement('div')
    );
    document.body.appendChild(instance.$el);
    app.config.globalProperties.$loginDialog = () => {
      instance.loginVisible = true;
    };
  }
};

// main.js
createApp(App).use(Antd).use(loginDialog).mount("#app"); 

被挂载的LoginDialogCom中使用了antdv的模态框,但a-modal无法正确被加载(Failed to resolve component: a-modal),查看elements还是a-modal标签
image

image.png1128×268 30.4 KB
访问↓查看代码
https://codesandbox.io/s/sleepy-tdd-4ycsb


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

你这样写是有问题的,LoginDialog.js 另外调用createApp,你ant组件只在main.js里面创建的app注册过。你应该使用app.defineComponent创建挂载组件


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...