<!--父组件Parent-->
<template>
<div>
<group>
<custom ref='child'></custom>
</group>
</div>
</template>
在组件内调用this.$refs.child返回VueComponent,是一个组件对象,就是组件<custom>的this。
在custom组件内,调用this.$parent,注意返回的是group的组件,而不是Parent组件。
<!--父组件Parent-->
<template>
<div>
<div>
<div>
<custom ref='child'></custom>
</div>
</div>
</div>
</template>
此时,在custom组件内,调用this.$parent,注意返回的是Parent的组件。
compute属性可以设置get/set属性
computed: {
fullName: {
// getter
get: function () {
return this.firstName + ' ' + this.lastName
},
// setter
set: function (newValue) {
var names = newValue.split(' ')
this.firstName = names[0]
this.lastName = names[names.length - 1]
}
}
}
现在再运行 vm.fullName = 'John Doe' 时,setter 会被调用,vm.firstName 和 vm.lastName 也会相应地被更新。