get: 首先会寻找 data 里得 fullName值,如果不存在,那么寻找computed里得,这里就找到了get
set: 只有改变 fullName里get 里存在的值话,那么就会重新改变fullName的值。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>计算属性的setter和getter</title>
<script src="../vue.js"></script>
</head>
<body>
<div id="app">
{{fullName}}
</div>
<script>
var vm = new Vue({
el: '#app',
data: {
firstName: 'Dell',
lastName: 'lee'
},
computed: {
fullName: {
get: function() {
return this.firstName + " " + this.lastName;
},
set: function (value) {
var arr = value.split(" ");
this.firstName = arr[0];
this.lastName = arr[1];
}
}
}
});
</script>
</body>
</html>