| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 
 | <body>
 <!--
 
 -->
 <div id="root">
 姓 <input type="text" v-model="firstName"> <br>
 名 <input type="text" v-model="lastName"> <br>
 <span>全名 {{fullname}}</span>
 </div>
 <script>
 Vue.config.productionTip = false //阻止vue在启动时生成生产提示
 const vm= new Vue({
 el: '#root',
 data: {
 firstName:'张',
 lastName:'三',
 fullname:'张三'//监视属性需要先创建一个变量便于后期修改
 },
 computed: {
 // fullname(){
 //     return this.firstName+'-' +this.lastName
 // }
 }
 ,
 methods: {
 changeishot(){
 this.ishot=!this.ishot
 }
 },
 //监视属性
 watch: {
 firstName(){
 //setTimeout直接由浏览器执行,其this指向为Windows,固需要使用箭头函数,this指向往上找
 setTimeout(() => {
 this.fullname=this.firstName+'-' +this.lastName
 }, 1000);
 },
 lastName(){
 //setTimeout直接由浏览器执行,其this指向为Windows,固需要使用箭头函数,this指向往上找
 setTimeout(() => {
 console.log(this)
 this.fullname=this.firstName+'-' +this.lastName
 }, 1000);
 }
 },
 
 })
 // // 第二种配置方法
 // vm.$watch('ishot',{
 //     immediate:true,
 //     handler(newValue,oldValue){
 //         console.log("ishot被改变了",newValue,oldValue);
 //     }
 // })
 </script>
 </body>
 
 |