计算属性
<!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>Document</title> <script src="../js/vue.js"></script> </head> <body> <div id="root"> 姓:<input type="text" v-model="firstname"> <br> <br> 名: <input type="text" v-model="lastname"> <br> <br> 姓名: <span>{{fullName}}</span> </div> <script> const vm = new Vue({ el: "#root", data: { firstname: '张', lastname: '三' }, computed: { //完整写法 // fullName: { // //当有人读取fullName时,get就会被调用且返回值作为fullName的值 // // get什么时候调用?初次读取fullName时;所依赖的数据发生变化 // get() { // console.log('get被调用了'); // return this.firstname + '-' + this.lastname // }, // set(value) { // console.log('set', value); // const arr = value.split('-'); // this.firstname = arr[0]; // this.lastname = arr[1]; // } // } // } //简写 fullName() { return this.firstname + this.lastname } } }) </script> </body> </html>
watch 属性