通常情况下只使用getter,获取数据
setter一般不用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<div id="app">
<!-- <h2>{{fullName}}</h2>-->
<h2>{{fullName}}</h2>
</div>
<script>
const app = new Vue({
el:"#app",
data:{
firstName:'yuan',
lastName:'huayuan'
},
computed:{
fullName:{
set(newValue){
console.log('-------------',newValue);
const names = newValue.split(' ');
this.firstName = names[0];
this.lastName = names[1];
},
get(){
return this.firstName+" "+this.lastName
}
}
}
})
</script>
</body>
</html>