img

父组件使用callback向子组件传值,解决子组件获取不到值的问题

2023-01-12 0条评论 1.4k次阅读 JavaScript


父组件使用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
},

💬 COMMENT


🦄 支持markdown语法

👋友