[vue] prop更新不及时问题 不是最新的

问题:父组件通过属性绑定更新了传递给子组件的prop属性 子组件却没有及时更新

父组件:通过props向子组件传递了一个数组info

当我点击父组件的按钮时,发请求获取了数据并赋值给了info数组,点击事件代码在下面(解决修改代码过程)

  <div class="">
    <button @click="btnClick">发送get请求</button>
    <Child :infoList="infoList" ref="child" />
  </div>
  
  data() {
    return {
      infoList: [],
    };
  },

父组件

    btnClick() {
      axios.get("http://localhost:8080/api/contactList").then((res) => {
        this.infoList = res.data;
        this.$refs.child.getNewestVal(); // 子组件获取到的infoList 不是最新的
      });
    },

子组件:通过props接收了父组件传过来的数组;它后一个方法,获取最新的数组值

 props: ["infoList"],

  methods: {
    getNewestVal() {
      console.log(this.infoList); // 子组件获取到的infoList 不是最新的
    },
  },

解决:this.$nextTick
父组件

    btnClick() {
      axios.get("http://localhost:8080/api/contactList").then((res) => {
        this.infoList = res.data.data;
        this.$nextTick(() => {
          this.$refs.child.getNewestVal();
        });
      });
    },

prop更新不及时问题

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值