dcloudio / dcloudio/uni-app

父组件向子组件中 props 一个函数,函数执行时的 this 为子组件实例

Open
#1,261 6 comments 0 reactions 1 assignee Claimed by @fxy060608 View on GitHub
bug
Dominant language
JavaScript
Stars
41.6k
Forks
3.7k
Avg merge
7h 6m
Merged PRs (30d)
6

Description

**问题描述**
父组件向子组件中 props 一个函数,函数执行时的 this 为子组件实例

**复现步骤**
子组件:child.vue
```


click

export default {
name: 'Child',

props: {
onClick: Function,
},

data() {
return {
id: 'child',
}
},

methods: {
onBtnClick() {
this.onClick()
},
},
}

```

父组件
```



import Child from './child'

export default {
name: 'Parent',

components: {
Child,
},

data() {
return {
id: 'parent',
}
},

methods: {
onClick() {
console.log(this.id) // child
},
},
}

```

**预期结果**
父组件中props 的 onClick 方法的 this 实例指向父组件,log 输出 parent

**实际结果**
log 输出 child,实例指向子组件

**系统信息:**
- 发行平台: 微信小程序
- 操作系统: iOS 13.3
- uni-app版本: uni-app v2.4.2, uni-app cli v2.0.0-24220191115011
- 设备信息: iPhone8

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.