Vuetify :节流/去抖动 v-autocomplete
Vuetify : throttle / debounce v-autocomplete
我正在使用 Vuetify Autocomplete 和远程数据,我想限制/消除 API 调用(当用户输入文本时等待 500 毫秒调用 API在自动完成)。我该怎么做?
我看到了关于 debounce-search
属性的 Stack OverFlow post,但它对我不起作用,而且我没有看到任何关于此属性的 Vuetify 文档。
您可以向进行 API 调用的函数添加去抖动。可以使用 setTimeout
and clearTimeout
实现去抖动器,这样新的调用就会被延迟并取消任何挂起的调用:
methods: {
fetchEntriesDebounced() {
// cancel pending call
clearTimeout(this._timerId)
// delay new call 500ms
this._timerId = setTimeout(() => {
this.fetch()
}, 500)
}
}
这样的方法可以绑定到 v-autocomplete
的 watcher on the search-input
道具:
<template>
<v-autocomplete :search-input.sync="search" />
</template>
<script>
export default {
data() {
return {
search: null
}
},
watch: {
search (val) {
if (!val) {
return
}
this.fetchEntriesDebounced()
}
},
methods: { /* ... */ }
}
</script>
非常感谢。
有用。
这是我的代码(对地址进行地理编码):
<v-autocomplete
ref="refCombobox"
v-model="adresseSelectionnee"
:items="items"
:loading="isLoading"
:search-input.sync="search"
no-filter
hide-details
hide-selected
item-text="full"
item-value="address"
placeholder="Où ?"
append-icon="search"
return-object
dense
solo
class="caption"
clearable
hide-no-data
></v-autocomplete>
watch: {
search(val) {
if (!val) {
return;
}
this.geocodeGoogle(val);
}
},
methods: {
geocodeGoogle(val) {
// cancel pending call
clearTimeout(this._timerId);
this.isLoading = true;
// delay new call 500ms
this._timerId = setTimeout(() => {
const geocoder = new this.$google.maps.Geocoder();
geocoder.geocode({ address: val, region: "FR" }, (results, status) => {
if (status === "OK") {
this.adressesGoogle = results;
this.isLoading = false;
} else {
this.isLoading = false;
}
});
}, 500);
},
我正在使用 Vuetify Autocomplete 和远程数据,我想限制/消除 API 调用(当用户输入文本时等待 500 毫秒调用 API在自动完成)。我该怎么做?
我看到了关于 debounce-search
属性的 Stack OverFlow post,但它对我不起作用,而且我没有看到任何关于此属性的 Vuetify 文档。
您可以向进行 API 调用的函数添加去抖动。可以使用 setTimeout
and clearTimeout
实现去抖动器,这样新的调用就会被延迟并取消任何挂起的调用:
methods: {
fetchEntriesDebounced() {
// cancel pending call
clearTimeout(this._timerId)
// delay new call 500ms
this._timerId = setTimeout(() => {
this.fetch()
}, 500)
}
}
这样的方法可以绑定到 v-autocomplete
的 watcher on the search-input
道具:
<template>
<v-autocomplete :search-input.sync="search" />
</template>
<script>
export default {
data() {
return {
search: null
}
},
watch: {
search (val) {
if (!val) {
return
}
this.fetchEntriesDebounced()
}
},
methods: { /* ... */ }
}
</script>
非常感谢。 有用。 这是我的代码(对地址进行地理编码):
<v-autocomplete
ref="refCombobox"
v-model="adresseSelectionnee"
:items="items"
:loading="isLoading"
:search-input.sync="search"
no-filter
hide-details
hide-selected
item-text="full"
item-value="address"
placeholder="Où ?"
append-icon="search"
return-object
dense
solo
class="caption"
clearable
hide-no-data
></v-autocomplete>
watch: {
search(val) {
if (!val) {
return;
}
this.geocodeGoogle(val);
}
},
methods: {
geocodeGoogle(val) {
// cancel pending call
clearTimeout(this._timerId);
this.isLoading = true;
// delay new call 500ms
this._timerId = setTimeout(() => {
const geocoder = new this.$google.maps.Geocoder();
geocoder.geocode({ address: val, region: "FR" }, (results, status) => {
if (status === "OK") {
this.adressesGoogle = results;
this.isLoading = false;
} else {
this.isLoading = false;
}
});
}, 500);
},