小程序防抖
涉及到微信小程序原生开发,引入lodash包
-
新建终端,下载lodash包
cnpm i lodash.get lodash.debounce
-
修改源码:
找到node_modules下面的lodash.get下面的index.js, lodash.debounce同理。
var root = freeGlobal || freeself || Function('return this')();
替换为:
var root = { Array: Array, Date: Date, Error: Error, Function: Function, Math: Math, Object: Object, RegExp: RegExp, String: String, TypeError: TypeError, setTimeout: setTimeout, clearTimeout: clearTimeout, setInterval: setInterval, clearInterval: clearInterval };
-
npm构建一下
-
使用代码
import debounce from 'lodash.debounce'; onInput: debounce(function (e) { console.log('防抖处理后的输入:', e.detail.value); // 在这里执行你的逻辑(如搜索、请求等) }, 500),

浙公网安备 33010602011771号