在用Mpuve的时候,做一个搜索功能:一遍输入一遍请求后台返回搜索建议列表。在Vue工程里这是挺简单的一个功能:就是一边输入,一边监听value,当发生变化就立刻请求相关接口。
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <template> <input type="text" v-model="value" /> </template> <script> export default { data: () => ({ value: '' }), watch: { value(val) { this.loadSuggest(val) } }, methods: { loadSuggest(val) { console.log(val) } } } </script>
|
但是在mpvue你会发现:这样会导致闪屏以及输入框跳位。
因为在Mpvue里, 每次变更vlaue都会从微信小程序的 JSCore 进程,通过 setData 序列化成字符串后发送到 JSRender 进程。
issue其实也有相关问题解决,但是需要修改源码。我也尝试了也发现挺多问题,最终还是放弃使用。
https://github.com/Meituan-Dianping/mpvue/issues/639
后来发现: 函数去抖(debounce)可以解决。
函数去抖: 当调用动作n毫秒后,才会执行该动作,若在这n毫秒内又调用此动作则将重新计算执行时间。
具体的函数去抖(debounce)以及函数节流(throttle)这里就不作详细介绍了。具体可以看:
https://github.com/hanzichi/underscore-analysis/issues/20
回到项目
上面说到函数去抖可以解决了,那么如何优化输入框跳位的问题?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <template> <input type="text" v-model="value" /> </template> <script> export default { data: () => ({ value: '' }), watch: { value(val) { this.loadSuggest(val) } }, methods: { loadSuggest: debounce(async function(val) { console.log(val) }, 1000) } } </script>
|
函数去抖(debounce)以及函数节流(throttle)的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
export const throttle = function(fn, threshhold) { var last
var timer
threshhold || (threshhold = 250)
return function() {
var context = this var args = arguments
var now = + new Date()
if (last && now < last + threshhold) { clearTimeout(timer)
timer = setTimeout(function() { last = now fn.apply(context, args) }, threshhold)
} else { last = now fn.apply(context, args) } } }
export const debounce = function(fn, delay) {
var timer
return function() {
var context = this var args = arguments
clearTimeout(timer)
timer = setTimeout(function() { fn.apply(context, args) }, delay) } }
|