uni-app解决父组件向子组件传值,子组件created生命周期接收不到值
父组件页面需要向子组件传值
<Honer :sdid="schoolInfo.sdid"></Honer>
子组件页面需要使用父组件穿过来的值 并请求接口
props: {
sdid: {
type: Number / String,
require: true
}
},
created() {
this.getShHonors()
},
methods:{
getShHonors() {
//发起请求
getShHonors(this.sdid).then(res => {
console.log('接口获取成功', res)
this.honerList = res.data
})
}
}
此时,第一次访问组件时 this.sdid为null
这时需要子组件页面使用watch监听变量
watch: {
sdid: {
immediate: true, //立即执行
deep: true, //深度侦听复杂类型
handler(newval, oldval) {
console.log(newval, oldval);
if (newval) {
this.sdid = newval
//调用请求接口的方法
this.getShHonors()
}
}
}
},
此时已经解决父组件向子组件传值,子组件created生命周期接收不到值的问题