010——VUE中使用lodash庫減少watch對后台請求的壓力


<!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>

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM