·计算属性默认只有 getter,Vue.js也允许给计算属性设置setter。

<!DOCTYPE html>
<html lang=“en”>
<head>
<meta charset=“UTF-8”>
<meta name=“viewport” content=“width=device-width, initial-scale=1.0”>
<title>Document</title>
</head>
<body>
<div id=“app”>
<p>{{ fullName }}</p>
</div>
<script src=“lib/vue.js”></script>
<script>
var vm = new Vue({
el: ‘#app’,
data: {
firstName: ‘张’,
lastName: ‘三’
},
computed: {
// 默认书写方式:
/* fullName () {
return this.firstName + this.lastName;
} */
// 分开书写 getter 与 setter
fullName: {
get () {
return this.firstName + this.lastName;
},
set (newValue) {
console.log(newValue)
var nameArr = newValue.split(‘ ‘);
this.firstName = nameArr[0];
this.lastName = nameArr[1];
}
}
}
});
</script>
</body>
</html>