直接上代码
父组件
// 父组件
<template>
<div>我是父组件</div>
<child :func="testFunc" />
</template>
<script>
import child from './child'
export default {
components: {
child
},
data () {
return {
hello: 'world'
}
},
methods: {
testFunc() {
this.hello = 'xixi'
}
}
}
</script>
子组件
// 子组件
<template>
<button @click="func" />
</template>
<script>
export default {
props: {
func: Function
}
}
</script>
上面是根据我自己业务逻辑简化后的代码,就是上面同样的代码,在uniapp
和mpvue
中运行的结果居然是不一样的。
- 在
uniapp
中,函数testFunc
中的this
作用域是指向子组件child
的,所以hello
的值无法赋值成功。 - 在
mpvue
中,函数testFunc
中的this
作用域就是指向当前组件,函数执行没问题。