如何将实现共同接口的不同子组件传递给Vue 3中的父组件?

huangapple go评论71阅读模式
英文:

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: &#39;Link column&#39;,
     component: MyLinkComponent
  },
  {
     title: &#39;Button column&#39;,
     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 &quot;text&quot; prop, but link can also have &quot;url&quot;.


&lt;Table :columns=&quot;columns&quot; /&gt;

Table.vue

const props = defineProps({
   columns
})

&lt;template&gt;
  &lt;div v-for=&quot;(column, index) in props.columns&quot; :key=&quot;column.id&quot;&gt;
    &lt;component :is=&quot;props.columns[index]&quot; /&gt;
  &lt;/div&gt;
&lt;/template&gt;

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>



huangapple
  • 本文由 发表于 2023年6月8日 18:25:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76430909.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定