父组件使用callback向子组件传值,解决子组件获取不到值的问题
Sonder
2023-01-12
409字
1分钟
浏览 (1.9k)
父组件使用callback向子组件传值,解决子组件获取不到值的问题
父组件:father.vue
<son @getUserId="getUserId"></son>
methods: {
getUserId(callback) {
const uid = 1
callback(uid)
},
}
子组件:son.vue
created(){
this.$emit("getUserId",userId=>{
this.userId = userId;
console.log(`this.userId===>`, this.userId) // 1
})
console.log(`this.userId===>1111`, this.userId) // 1
},