watch監聽方法,watch可以監聽多個變量,具體使用方法看代碼:
HTML:
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>vue.js 使用watch監聽實現類似百度搜索功能 </title> <script src="vue.js"></script> <script src="node_modules/axios/dist/axios.js"></script> </head> <body> <div id="ask"><!--vue不能控制body和html的標簽--> <input type="text" v-model="word"> <h1>{{result}}</h1> </div> <script> var app = new Vue({ //實例化vue el:'#ask',//vue控制id為ask的元素, //watch 可以監聽多個變量 watch:{ //監聽word變量 word:function (newV,oldV) { console.log('舊值:'+oldV+'=======>新值:'+newV); //這里可以寫異步請求我用的是axios axios.get('Api.php?word='+newV).then(function (res) { console.log(res,'這是異步返回的值'); //這里寫異步得到值之后的動作 app.result=res.data; }); } }, data:{ word:'', result:'' } }); </script> </body> </html>