动态组件
<template>
<component :is="currentComponent" v-bind="passedProps"></component>
</template>
<script>
export default {
props: {
load: Function,
props: Object,
},
data() {
return {
currentComponent: null,
loadedComponent: null,
};
},
computed: {
passedProps() {
return this.props;
},
},
watch: {
load(newLoad, oldLoad) {
this.loadComponent(newLoad);
},
},
methods: {
loadComponent(loadFunc) {
loadFunc()
.then((component) => {
if (!component.default) {
console.error("Loaded module does not contain a default export");
this.currentComponent = null;
return;
}
this.currentComponent = component.default;
})
.catch((err) => {
console.error("Failed to load component:", err);
this.currentComponent = null;
});
},
},
mounted() {
this.loadComponent(this.load);
},
};
</script>