在vue3中消除可组合混音

axkjgtzd  于 2021-09-23  发布在  Java
关注(0)|答案(0)|浏览(243)

我在vue3中使用了可组合的去盎司函数,而不是mixin。但它不会像预期的那样工作(取消超时函数调用)。它是清除超时,当取消公告的函数调用cleartimeout时,timeouted函数立即调用。

  1. export default function useDebounceMethod() {
  2. const debounce = function(fn, interval) {
  3. let timer;
  4. return function debounced() {
  5. clearTimeout(timer);
  6. let args = arguments;
  7. let that = this;
  8. timer = setTimeout(() => {
  9. fn.apply(that, args);
  10. }, interval);
  11. };
  12. }
  13. return {
  14. debounce
  15. }
  16. }

然后像这样使用:

  1. import useDebounceMethod from "@/composables/mixin";
  2. export default {
  3. name: "Search",
  4. components: {},
  5. setup() {
  6. // only instead of mixins
  7. const { debounce } = useDebounceMethod();
  8. return {
  9. debounce,
  10. };
  11. },
  12. data() {
  13. const dsFunction = this.debounce(function () {
  14. this.setRepositories();
  15. }, 1000);
  16. return {
  17. debouncedSearch: dsFunction,
  18. };
  19. },
  20. ...

稍后致电:

  1. this.debouncedSearch()

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题