<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<style>
.active {
color: red;
}
</style>
<body>
<div id="app">
<h2>总价格:{{fullname}}</h2>
<h2>{{getfullname()}}</h2>
</div>
<script src="./js/vue.js"></script>
<script>
var app = new Vue({
el: '#app',
data: {
firstname: 'geyao',
lastname: 'hello'
},
methods: {
getfullname: function() {
return this.firstname + ' ' + this.lastname
}
},
computed: {
name: 'codewhy',
fullname: {
set: function(newValue) {
console.log('------------', newValue)
const names = newValue.split(' ');
this.firstname = names[0];
this.lastname = names[1];
},
get: function() {
return this.firstname + ' ' + this.lastname
}
}
}
})
</script>
</body>
</html>
运行结果