【发布时间】:2020-09-14 00:23:25
【问题描述】:
我们有一个 Vue 应用程序,并希望允许第三方创建插件。我们希望插件以 Vue 单文件组件的形式构建。
在运行时,最终用户会选择一个插件添加到应用程序中。该应用程序将获取纯文本 .vue 文件,即时编译它,并将其显示在应用程序中。
Vue 支持dynamic and async components,但这些需要提前编译到应用程序中。我们想做同样的事情,除了动态加载代码。
我怎样才能做到这一点?
这是我目前所得到的:
<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> 想要的。
【问题讨论】:
-
这应该做你想做的...markus.oberlehner.net/blog/…
-
@mark-hahn 这就是答案。谢谢。
-
看看这个答案:stackoverflow.com/a/42020118/138579(免责声明:作者在这里)
标签: javascript vue.js vue-dynamic-components