vue-dynamic-component

使用动态组件实现菜单加载逻辑,并通过 mixin 提取公用部分。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<template>
<div>
<component
:is="item"
v-for="(item, index) in components"
:key="item + index"
:ref="item"
/>
</div>
</template>
<script>
import Vue from 'vue';
import { mapActions } from 'vuex';
export default {
props: {
components: {
type: Array,
},
},
created() {
if (this.components && this.components.length > 0) {
this.components.forEach(component => {
Vue.component(component, resolve =>
require([`@/views/map/dynamicComponents/${component}`], response => {
response.default.mixins = response.default.mixins || [];
response.default.mixins.push({
created() {
this.$store.dispatch('AddDynamicComponent', {
name: component,
handleClose: this.handleClose,
});
},
});
resolve(response);
}, () => {
console.error(`未找到 @/views/map/dynamicComponents/${component}`);
})
);
});
}
},
methods: {
...mapActions(['AddDynamicComponent']),
},
};
</script>