<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>计算属性的Setter</title>
<script src="./js/vue.global.js"></script>
</head>
<body>
<div id="app">
<div> {{ firstName }} </div>
<div> {{ lastName }} </div>
<div> {{ fullName }} </div>
</div>
<script>
const vm = Vue.createApp({
data() {
return {
firstName: 'Ang',
lastName: 'Li'
}
},
computed: {
fullName: {
// getter
get() {
return this.firstName + ' ' + this.lastName;
},
// setter
set(newValue) {
const names = newValue.split(' ');
this.firstName = names[0];
this.lastName = names[names.length-1];
}
}
}
}).mount('#app');
vm.fullName = 'Li Lai';
</script>
</body>
</html>
Vue计算属性的Setter
最新推荐文章于 2022-08-28 21:43:30 发布