vue3 父子组件传递数组
时间: 2023-11-15 08:55:12 浏览: 143
在 Vue3 中,可以通过 props 将数组从父组件传递到子组件。具体步骤如下:
1. 在父组件中定义一个数组,并将其作为 props 传递给子组件:
```html
<template>
<div>
<child-component :myArray="parentArray"></child-component>
</div>
</template>
<script>
import ChildComponent from './ChildComponent.vue';
export default {
components: {
ChildComponent,
},
data() {
return {
parentArray: [1, 2, 3],
};
},
};
</script>
```
2. 在子组件中,通过 props 接收父组件传递的数组,并在模板中使用:
```html
<template>
<div>
<ul>
<li v-for="item in myArray" :key="item">{{ item }}</li>
</ul>
</div>
</template>
<script>
export default {
props: {
myArray: {
type: Array,
required: true,
},
},
};
</script>
```
这样就可以在子组件中使用父组件传递的数组了。
阅读全文
相关推荐















