父组件触发子组件事件时,需要传值过去,但是存在了数据异步的问题
父组件:
当在父组件直接使用this.$refs.childleft 来触发子组件中的方法时,父组件传过去的yearvalue值会有异步的情况出现
解决办法有两种,第一种是在父组件中直接用this.$nextTick方法把事件包裹起来;
// 触发年份选择器
handleChangeyear(value){
var that = this
that.str1 = value
that.$nextTick(() => {
that.$refs.childleft.handleMoods()
});
},
第二种办法是在子组件中先用props方法拿到yearvalue得值,再用watch方法监听yearvalue值得变化。个人推荐使用watch监听
props: {
yearvalue: String,
},
watch:{
yearvalue(){
// 调用需要触发yearvalue的方法
this.handleMoods()
}
},