加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

vue中子组件的methods中获取到props中的值方法

发布时间:2020-12-17 02:12:28 所属栏目:百科 来源:网络整理
导读:父子组件通信 这个官网很清楚,也很简单,父组件中使用v-bind绑定传送,子组件使用props接收即可 例如: 父组件中 {{number}} {{amount}} {{profits}} 子组件中 这种情况下,子组件的methods中想要取到props中的值,直接使用this.chartData即可 但是有写情况

父子组件通信

这个官网很清楚,也很简单,父组件中使用v-bind绑定传送,子组件使用props接收即可

例如:

父组件中

{{number}}
{{amount}}
{{profits}}

子组件中

这种情况下,子组件的methods中想要取到props中的值,直接使用this.chartData即可

但是有写情况下,你的chartData里面的值并不是固定的,而是动态获取的,这种情况下,你会发现methods中是取不到你的chartData的,或者取到的一直是默认值

比如下面这个情况

父组件中

data(){ return { number: null,chartData: [] } },mounted(){ this.getStatistics(); },methods: { //获取统计数据 getStatistics(){ console.log('获取统计数据') axios.post(api,{
    }).then((res) => {
      this.number = res.data.domain.list[0].number;
      this.amount = res.data.domain.list[0].amount;
      this.profits = res.data.domain.list[0].profits;
      this.chartData = [this.number,this.amount,this.profits];
    }).catch((err) => {
      console.log(err);
    })
  },},</script>

此时子组件的methods中使用this.chartData会发现是不存在的(因为为空了)

这情况我是使用watch处理

解决方法如下:

使用watch

监听chartData的值,当它由空转变时就会触发,这时候就能取到了,拿到值后要做的处理方法也需要在watch里面执行

以上这篇vue中子组件的methods中获取到props中的值方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程之家。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读