1. 程式人生 > >lodash庫減少vue watch鑑定axios後臺請求的壓力

lodash庫減少vue watch鑑定axios後臺請求的壓力

<script src="vue.js"></script>
<script src="node_modules/axios/dist/axios.js"></script>
<script src="node_modules/lodash/lodash.js"></script>
<script>
    var app=new Vue({
        el:'#div',
        watch:{
            //使用debounce時,需要使用:npm install lodash安裝lodash。
            word:_.debounce(
                function(x,j){
                    console.log(x+'=>'+j)
                    axios.get('index.php?word='+x).then(function(response){
                        console.log(response.data)
                        console.log(response.status)
                        app.result=response.data
                    })
                 },100
            )
        },
        data:{
            word:'',
            result:''
        }
    })
</script>