vue中子组件的created、mounted钩子中获取不到props中的值问题
父子组件通信
这个官网很清楚,也很简单,父组件中使用v-bind绑定传送,子组件使用props接收即可
例如:
父组件中:
<template>
<div>
<head-top></head-top>
<section class="data_section">
<header class="chart-title">数据统计</header>
<el-row :gutter="20" class="chart-head">
<el-col :xs="24" :sm="12" :md="6" :lg="6"><div class="grid-content data-head blue-head">统计:</div></el-col>
<el-col :xs="24" :sm="12" :md="6" :lg="6"><div class="grid-content data-head">销售数量 <span>{{number}}</span></div></el-col>
<el-col :xs="24" :sm="12" :md="6" :lg="6"><div class="grid-content data-head">销售金额 <span>{{amount}}</span></div></el-col>
<el-col :xs="24" :sm="12" :md="6" :lg="6"><div class="grid-content data-head">利润统计 <span>{{profits}}</span></div></el-col>
</el-row>
</section>
<chart :chartData="chartData"></chart>
</div>
</template> <script>
data(){
return {
number: null,
amount: null,
profits: null,
chartData: [10,10,10]
}
},
</script>
子组件中:
export default {
props: ['chartData']
}
这种情况下,子组件的 methods 中想要取到props中的值,直接使用 this.chartData 即可
但是有写情况下,你的 chartData 里面的值并不是固定的,而是动态获取的,这种情况下,你会发现 methods 中是取不到你的 chartData 的,或者取到的一直是默认值。
比如下面这个情况
父组件中:
<script>
data(){
return {
number: null,
amount: null,
profits: 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 :
export default {
props: ['chartData'],
data(){
return {
cData: []
}
},
watch: {
//正确给 cData 赋值的 方法
chartData: function(newVal,oldVal){
this.cData = newVal; //newVal即是chartData
newVa l&& this.drawChart(); //newVal存在的话执行drawChar函数
}
},
methods: {
drawChart(){
//执行其他逻辑
}
},
mounted() {
//在created、mounted这样的生命周期, 给 this.cData赋值会失败,错误赋值方法
// this.cData = this.chartData;
}
}
监听 chartData 的值,当它由空转变时就会触发,这时候就能取到了,拿到值后要做的处理方法也需要在 watch 里面执行。
vue中子组件的created、mounted钩子中获取不到props中的值问题的更多相关文章
- Vue中子组件调用父组件的方法
Vue中子组件调用父组件的方法 相关Html: <!DOCTYPE html> <html lang="en"> <head> <meta ...
- asp.net 类库中获取session c#类中获取session
asp.net 类库中获取session c#类中获取session 1. 先引入命名空间 using System.Web; using System.Web.SessionState; 在使用H ...
- Web版需求征集系统所得1,servlet中获取checkbox复选框的值
servlet中获取checkbox复选框的值 </tr> <tr> <td align="right">研究类型</td> < ...
- 【Android】12.3 在当前Activity中获取另一个Activity的返回值
分类:C#.Android.VS2015: 创建日期:2016-02-23 一.简介 在上一节的示例中,通过StartActivity(Intent)方法启动另一个Activity后,这两个Activ ...
- 从O365中获取users到D365中 使用flow
在我上篇blog中讲解到了怎么用代码把O365 users 获取到D365中. 从O365中获取users到D365中 这几天一直在研究flow, 发现flow可以更简单的完成这个功能. 一开始没有考 ...
- vue中子组件的methods中获取到props中的值
这个官网很清楚,也很简单,父组件中使用v-bind绑定传送,子组件使用props接收即可 例如: 父组件中 <template> <div> <head-top>& ...
- vue中子组件的拆分 父组件与子组件之间的传值
vue是组件式开发,尽量独立出子组件 prop():父组件传值给子组件 $emit():子组件传值给父组件 子组件中的设置: 使用bind <template> : default-che ...
- Vue中子组件数据跟着父组件改变和父组件数据跟着子组件改变的方法
一,子组件数据跟着父组件改变 父组件的代码 <template> <div class="home"> <img alt="Vue logo ...
- 埋坑一: vue中子组件调用兄弟组件方法
小计: 开发中遇到子组件需要调用兄弟组件中的方法,如下写个小demo记录下心得,如果你有好的方法,请到评论区域指教 父组件示例代码: 组件功能解析: 通过$emit获取子组件事件,通过$ref调用子组 ...
随机推荐
- 【Canvas】绘制几何级数Geometric series曲线 y=1+1/2+1/4+1/8+1/16+1/32+1/64+....
相关资料:https://baike.baidu.com/item/%E5%87%A0%E4%BD%95%E7%BA%A7%E6%95%B0/112584?fr=aladdin 图线: 代码: < ...
- Linux 基于WEB开源的系统管理工具webmin
Webmin是目前功能最强大的基于Web的Unix系统管理工具.管理员通过浏览器访问Webmin的各种管理功能并完成相应的管理动作.目前Webmin支持绝大多数的Unix系统,这些系统除了各种版本的l ...
- Tosca database help link
https://support.tricentis.com/community/manuals_detail.do?lang=en&version=12.0.0&url=tosca_b ...
- 2013年各大小IT公司待遇,绝对真实,一线数据!(初版)
本人西电硕士,根据今年找工作的情况以及身边同学的汇总,总结各大公司的待遇如下,吐血奉献给各位学弟学妹,公司比较全,你想去的公司不在这里面,基本上是无名小公司了:无名小公司有时也很给力哦以下绝对是各大公 ...
- python 中的一些基础算法:递归/冒泡/选择/插入
递归算法 如果一个函数包含了对自己的调用,那么这个函数就是递归的. 比如我们计算下1-7乘法的计算: def func(n): if n ==1 : return 1 return n*func(n- ...
- PAT 甲级 1035 Password (20 分)(简单题)
1035 Password (20 分) To prepare for PAT, the judge sometimes has to generate random passwords for ...
- Windows .net服务器系列 wmic命令使用示例--Examples of WMIC commands for Windows .NET SERVER Family
1.0 Method execution: NICCONFIG (Win32_NetworkAdapterConfiguration) WMIC NICCONFIG WHERE Index=1 CAL ...
- (二)第一个Servlet
一.预备知识 一.Servlet简介 Servlet是sun公司提供的一门用于开发动态web资源的技术. Sun公司在其API中提供了一个servlet接口,用户若想用发一个动态web资源(即开发一个 ...
- 【Leetcode_easy】728. Self Dividing Numbers
problem 728. Self Dividing Numbers solution1: 使用string类型来表示每位上的数字: class Solution { public: vector&l ...
- Linux配置代理IP
Linux配置代理IP: vim /etc/profile http_proxy=http://username:password@ip:port/ https_proxy=http://userna ...