computed
属性,是一个对象,里面的属性,其实是个方法,但我们调用的时候,不要加()
来调用否则会报错,在computed
属性中定义的属性,就不用在data
身上定义了
<!DOCTYPE html>
<html lang="zh-CN">
<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>
<script src="js/vue.js"></script>
</head>
<body>
<div id="app">
<input type="text" v-model='firstname'>
<input type="text" v-model='lastname'>
<input type="text" v-model='fullname'>
</div>
<script>
var vm = new Vue({
el: '#app',
data: {
firstname:'',
lastname: ''
},
methods: {},
computed: {
fullname: function() {
return this.firstname + this.lastname
}
}
});
</script>
</body>
</html>
网友评论