【发布时间】:2020-12-21 00:31:58
【问题描述】:
是否可以在运行时动态加载 vue 组件(在电子应用中构建插件系统)?
- 组件位于特定文件中
- 它的路径只有在运行时才知道
- 组件既可以预编译(如果可能,不知道)或在运行时编译
- 下面列出了一个简单的示例组件
我尝试了以下方法,都失败了:
-
需要组件
<template> <component :is="currentComp"></component> </template> <script> ... methods: { loadComponent(path) { const dynComp = eval('require(path)'); // use eval to prevent webpackresolving the require this.currentComp = dynComp; } }, ... </script>导入成功,但
this.currentComp = dynComp;行失败并显示错误消息:Error in data(): "Error: An object could not be cloned." -
使用 here 提供的代码,但将 url 替换为本地路径
失败并显示错误消息:
Failed to resolve async component: function MyComponent() { return externalComponent('/path/to/Component.vue'); } Reason: TypeError: Chaining cycle detected for promise #<Promise>
使用的示例组件如下:
// Example component
module.exports = {
template: `
<div>
<input v-model="value"/>
<button @click="clear">Clear</button>
<div>{{ value }}</div>
</div>`,
name: 'Example',
data() {
return {
value: '',
};
},
watch: {
value(value) {
console.log('change!');
},
},
methods: {
clear() {
this.value = '';
},
},
};
【问题讨论】:
标签: vue.js plugins electron components