<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>使用lodash庫減小watch對後臺請求的壓力</title> <script src="vue.js"></script> <script src="node_modules/axios/dist/axios.js"></script> <script src="node_modules/lodash/lodash.js"></script> </head> <body> <div id="lantian"> <input type="text" v-model="word"/> <h1> 結果:{{result}} </h1> </div> <script> var app = new Vue({ el: '#lantian', watch: { //使用debounce時,須要使用:npm install lodash安裝lodash。 word: _.debounce( function (newV, oldV) { axios.get('9.php?word=' + newV).then(function (response) { console.log(response); app.result = response.data; }); }, 3000 ) }, data: { word: '', result: '' } }) ; </script> </body> </html>