我们有一个 Vue 应用程序,并希望允许第三方创建插件。我们希望插件以 Vue 单文件组件的形式构建。
在运行时,最终用户会选择一个插件添加到应用程序中。该应用程序将获取纯文本 .vue 文件,即时编译它,并将其显示在应用程序中。
Vue 支持动态和异步组件,但这些组件需要提前编译到应用程序中。我们想做同样的事情,除了动态加载代码。
我怎样才能使这项工作?
这是我到目前为止所得到的:
<template>
<div>
The component goes here:
<component :is="pluginComponent"></component>
</div>
</template>
<script>
import { parseComponent } from "vue-template-compiler";
export default {
data() {
return {
pluginComponent: null
};
},
mounted() {
// fetch code from some external source here
let code = "<template><div>hello</div></template>";
let comp = parseComponent(code);
this.pluginComponent = comp;
}
};
</script>
(我修改了构建,因此存在 vue-template-compiler。)
上面的代码会产生这个错误:
[Vue warn]: invalid template option:[object Object]
found in
---> <Anonymous>
<Pages/plugin/PluginId.vue> at pages/plugin/_plugin_id.vue
<Nuxt>
<Layouts/default.vue> at layouts/default.vue
<Root> instrument.js:110
instrumentConsole instrument.js:110
VueJS 15
TypeError: "vnode is null"
VueJS 14
instrument.js:110
我猜想无论 parseComponent() 产生什么都不<component>
是我们想要的。