英文:
How to pass different child components that implement the common interface to a parent component in Vue 3?
问题
我正在寻找一种在表格内呈现不同组件的方法。表格的每一列只能包含相同类型的组件。
我想要实现的主要思路(伪代码):
App.vue
const columns = [
{
title: '链接列',
component: MyLinkComponent
},
{
title: '按钮列',
component: MyButtonComponent
}
]
// MyLinkComponent 和 MyButtonComponent 有不同的模板和属性,但一些属性是相同的(通用公共接口)。例如,
// 它们都可以有 "text" 属性,但链接还可以有 "url" 属性。
<Table :columns="columns" />
Table.vue
const props = defineProps({
columns
})
<template>
<div v-for="(column, index) in props.columns" :key="column.id">
<component :is="props.columns[index]" />
</div>
</template>
我该如何实现这个目标?
英文:
I am looking for a way to render different components inside a table. Each column of the table can include only the same component type.
The main idea what I want to get (pseudo code):
App.vue
const columns = [
{
title: 'Link column',
component: MyLinkComponent
},
{
title: 'Button column',
component: MyButtonComponent
}
]
// MyLinkComponent and MyButtonComponent have different templates and props,
// but some of props are the same (generic public interface). For example
// they both can have "text" prop, but link can also have "url".
<Table :columns="columns" />
Table.vue
const props = defineProps({
columns
})
<template>
<div v-for="(column, index) in props.columns" :key="column.id">
<component :is="props.columns[index]" />
</div>
</template>
How can I achieve that?
答案1
得分: 1
请注意在Table.vue中注册您的列组件 全局或局部:
例如,在Table.vue中,请确保已注册组件;
<template>
<div
v-for="(column, columnIndex) in columns"
:key="columnIndex"
>
<component :is="column.componentName" v-bind="column.props" />
</div>
</template>
<script>
import MyLinkComponent from '...';
import MyButtonComponent from '...';
export default {
components: { MyLinkComponent, MyButtonComponent },
props: {
columns: {
type: Array,
default: () => []
}
}
};
</script>
您可以在Table.vue中更改数组的结构以使其更可读。
App.vue
const columns = [
{
componentName: 'MyLinkComponent',
props: {
title: '链接列'
}
},
{
componentName: 'MyButtonComponent',
props: {
title: '按钮列'
}
}
]
<details>
<summary>英文:</summary>
You need to register your column components [globally or local][1]:
Ex. Table.vue, please make sure that the components are registered;
<template>
<div
v-for="(column, columnIndex) in columns"
:key="columnIndex"
>
<component :is="column.componentName" v-bind="column.props" />
</div>
</template>
<script>
import MyLinkComponent from '...';
import MyButtonComponent from '...';
export default {
components: {MyLinkComponent, MyButtonComponent},
props: {
columns: {
type: Array,
default: () => []
}
}
};
</script>
[1]: https://vuejs.org/guide/components/registration.html#global-registration
You can change the structure of the array to be more readable at Table.vue
App.vue
const columns = [
{
componentName: 'MyLinkComponent',
props: {
title: 'Link column'
}
},
{
componentName: 'MyButtonComponent',
props: {
title: 'Button column'
}
}
]
</details>
# 答案2
**得分**: 1
I'm sorry, but I can't provide the translation for the code you've provided. Please provide the text you'd like me to translate, and I'll be happy to assist you with that.
<details>
<summary>英文:</summary>
checkout the demo, it works fine.
https://play.vuejs.org/#eNqtVE1v2zAM/SuELm2BzAHSm+cWWIceNgxb0fVW7RDYdKbGlgSJTlMY/u+j5K8269ehOSSW+Ei+Rz6nFV+sTXYNilRkPnfKEnikxp5LrWprHMEN7glKZ2qQIlmGU8BL8QSweopYHUC+cmwC8CG2DHGpc6M9QW6qptYezuBWaoA2fPGHFFWYwtEPpbcD5mgxxHIuYzRqSiODcNvF2GHyRUNk9Bvpq5gv9Z/AKVv2o+Ah8IGwttWakE9ZEJIOZM+kGJ6kgGWILieoWAjyLK1Um+TOG83jjbRCSm1Vhe6XJcXSpUhHwlKsq8rcf4935BocqHLOX8y3z9zf+X24k+LKoUe345FOMVq7DVIfvvz9M0xoDtamaCpGvxK8Rs/iAscedtHogmk/wkW23+KCld7c+Ms9ofajqEB03okUvPCw+Jekz3RPk9NhFx1PcTTLoT+hWusNb4C4zOxVerAIbciJq+1G00W3fWaYJnTlOke4Nvd988ElnhyLCBezMaY6TGW2qnXGBqMWWCqNV+GUxUqDGdJQ+5Z91J0fn7zqJoCsUDvYfSqNYynHfYEFKF3g/oR/+l7J7LJ0iw+T7ZJIPcrvp5hN1CFVsz05f9TR+zT6H9r2cZmOFXKFJRP6z8njO//GDt6jNT6Nr+vL3eL/x0e2i6/3c/26f+mAtEU=
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论