vue使用节流函数
2021/09/30    标签: vue    lodash    immediate    debounce   
<html>
    <head>
        <script type="text/javascript" src="https://unpkg.com/vue@3.2.19/dist/vue.global.js"></script>
        <script type="text/javascript" src="https://cdn.staticfile.org/lodash.js/4.17.21/lodash.min.js"></script>
    </head>
    <body>
        <div id="app">
            <input type="text" v-model="name"/>
        </div>
        <div id="app_immediate">
            <input type="text" v-model="name"/>
        </div>
    </body>
    <script type="text/javascript">
        Vue.createApp({
            data(){
                return {
                    name:"li"
                }
            },
            methods:{
                
            },
            watch:{
                name:_.debounce(function(newValue,oldValue){
                    console.log(newValue,oldValue);
                },500)
            }
        }).mount('#app');
        Vue.createApp({
            data(){
                return {
                    name:"li"
                }
            },
            methods:{
                
            },
            watch:{
                name:{
                    handler:_.debounce(function(newValue,oldValue){
                        console.log(newValue,oldValue);
                    },500),
                    deep:true,
                    immediate:true
                }
            }
        }).mount('#app_immediate');
    </script>
</html>