vue父组件如何向子组件中传递数据?

原文地址


props传参

父组件:

<template>
    <parent>
        <child :list="list"></child> //在这里绑定list对象
    </parent>
</template>
import child from 'child.vue';
export default{
components:{child},
  data(){
   return {
   //父组件的数据绑定到子组件的包裹层上
  list:["haha","hehe","xixi"];
}
}

子组件:(子组件要嵌套到父组件中)

child.vue
<template>
   <ul>
        <li v-for="(item ,index)in list">{{item}}</li>
    </ul>
</template>
export default{
     props:{
    list:{
      type:Array,//type为Array,default为函数
      default(){
        return [
          "hahaxixihehe"//默认初始值
        ]}}
    },//用props属性进行传数据,此时子组件已经获取到list的数据了 data(){ return {} } }

返回目录

猜你喜欢

转载自www.cnblogs.com/gitByLegend/p/10870970.html