有条件地定义了VueJS中的导入组件
可以说,我在VUEJS中有一个父母组件,其中有两个版本的子组件(a,b)。 有办法做这样的事情吗?动态定义组件,而不是使用V-IF和V-ELSE?
<template>
<Child />
</template>
<script>
import A from './components/A.vue';
import B from './components/B.vue';
export default {
name: 'Parent',
components: {
Child: () => {
if (renderA === true) {
return A;
} else {
return B;
}
},
},
computed: {
renderA() {
return Math.random() < 0.5;
},
}
}
</script>
Lets say i have a parent component in VueJs with two versions of child component (A, B).
Is there a way to do something like this? Dynamically define component rather than using v-if and v-else?
<template>
<Child />
</template>
<script>
import A from './components/A.vue';
import B from './components/B.vue';
export default {
name: 'Parent',
components: {
Child: () => {
if (renderA === true) {
return A;
} else {
return B;
}
},
},
computed: {
renderA() {
return Math.random() < 0.5;
},
}
}
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
&lt;组件&gt;
标签。 vue docsYou can use the
<component>
tag. Vue Docs