vue组件父传子参数,参数还没赋值完成就被传递,传递了空参数

1、父组件传给子组件
在子组件里定义一个props,即props:[‘msg’],msg可以是对象也可以是基本数据类型,
也可定义一个默认值,即 props:{msg: {type: String, default: ‘hello world’}},

Children.vue

<template>
</template>

<script>
    export default {
        name: "Children",
        components: {},
        props:['msgList'],
        created() {
    		console.log('----父组件传过来的消息是===')
    		console.log(this.msgList)
    		console.log('--==')

  },
        data() {
            return {
               
            }
        },
        methods: {}
    }
</script>

Parent.vue

<template>
  <div class="parent">
    <Children :msgList="msgList"></Children>
  </div>
</template>

<script>
import Children from '../components/Children'

export default {
  name: 'Parent',
  components: {
      Children
  },
  created() {
    this.getList();
    console.log("----parent--")
    console.log(this.msgList)
    console.log("000000")
  },
  data() {
      return {
          msgList:[],
 	  }
 },
  getList() {
      getServerList().then(res => {
        console.log("res=====")
        this.serverList = res.data;
        console.log(res)
        console.log(this.serverList)
      })
  },


}
</script>

这个时候,会发现父组件传递给子组件的参数是空数组,而不是经过getList()赋值后的数据。
这个时候,只需要在调用子组件的标签那里添加一个v-if


Parent.vue

<template>
  <div class="parent">
    <Children :msgList="msgList" v-if="msgList.length>0"></Children>
  </div>
</template>

使用了v-if这个时候,如果要传递的数组为空,则不传递参数;不为空再传递,就解决了 参数还没来得及赋值,就被传递的问题。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

北海南风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值