父组件给子组件传值的两种方法

方法有两种,

方法一:

props传值,这里注意一个问题,传过来的值需要用watch监听并赋值,否则这里获取到的是空数组

  父组件:

1

<uploadImg :width="200" :height="200" name="productImage" size="750px*750px" ref="productImage" :src-list="this.productImage"></uploadImg>

1

this.productImage=res.data.cover;

这里把通过后台返回的数组赋值给

this.productImage,然后把该数组传给子组件定义的props属性src-list

子组件:

1

2

3

4

5

6

7

8

watch:{

     srcList(curVal,oldVal){

      if(curVal){

       ;

       this.uploadImg=curVal;

       }

     },

}

然后子组件成功获取到该数组

 

 

方法二:

通过ref属性,父组件调用子组件的方法,把要传的数组作为参数传给子组件,子组件获取该参数,并使用

父组件:

1

this.$refs.productImage.getSrcList(res.data.cover);

子组件:

1

2

3

getSrcList(val){

  this.uploadImg=val;

}


版权声明:本文为weixin_41883384原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。