相关疑难解决方法(0)

如何在 Vue 3 中以编程方式创建组件实例?

我有一个用于常见场景的 Vue 2 模式:以编程方式创建一个实例以在模板外部的动态内容上打开模态/对话框/灯箱。

在 Vue 2 中,我发现了这种模式:

// DialogService.js

export default {
  alert(text) {
    const DialogClass = Vue.extend(DialogComponentDef);
    let dialog = new DialogClass({ propsData: { text } });

    dialog.$on('close', () => {
      dialog.$destroy();
      dialog.$el.remove();
      dialog = null;
    });

    // mount the dynamic dialog component in the page
    const mountEl = document.createElement('div');
    document.body.appendChild(mountEl);
    dialog.$mount(mountEl);
  },
};
Run Code Online (Sandbox Code Playgroud)

我怎样才能在 Vue 3 中实现这一点,知道Vue.extends, $on&$destroy不再存在?您可以单击此处查看 DialogService.js 的完整示例。

javascript vue.js vue-component vuejs2 vuejs3

4
推荐指数
1
解决办法
1万
查看次数

标签 统计

javascript ×1

vue-component ×1

vue.js ×1

vuejs2 ×1

vuejs3 ×1