<div id="enjoy">
<!--lucy bob-->
<p>{{fullName}}</p>
<!--john tom-->
<button @click="deal()">调用setter方法</button>
</div>
</div>
{
new Vue({
el: '#enjoy',
data: {
firstName: 'lucy',
lastName: 'bob'
},
computed: {
// //get
// fullName(){
// return this.firstName+' '+this.lastName;
// }
fullName: {
//get方法
get() {
return this.firstName + ' ' + this.lastName;
},
//set方法
set(str) {
let nameArr = str.split(' ');
this.firstName = nameArr[0];
this.lastName = nameArr[1];
}
}
},
methods: {
deal() {
//调用fullName的setter方法
this.fullName = 'john tom';
}
}
})
}