javascript - VueJS 2 Uncaught ReferenceError: _ is not defined with debounce - Stack Overflow

I'm trying to debounce a function from running for 500ms. Following the documentation here:.html#

I'm trying to debounce a function from running for 500ms. Following the documentation here:

.html#debounce-Param-Attribute-for-v-model-removed

    methods: {
        // Get the data needed for this page
        fetchData: _.debounce(function () {
            this.$http.get('widgets/quickfindordernumber/' + this.quickFindOrderNumber).then(function (response) {
                console.log(response.body)
            }, function (error) {
                console.log(error);
            });
        }, 500)
    }

But when running this function I get an error in the console Uncaught ReferenceError: _ is not defined I have tried removing the _. in front of debounce but it says debounce is not defined either.

I'm trying to debounce a function from running for 500ms. Following the documentation here:

https://v2.vuejs/v2/guide/migration.html#debounce-Param-Attribute-for-v-model-removed

    methods: {
        // Get the data needed for this page
        fetchData: _.debounce(function () {
            this.$http.get('widgets/quickfindordernumber/' + this.quickFindOrderNumber).then(function (response) {
                console.log(response.body)
            }, function (error) {
                console.log(error);
            });
        }, 500)
    }

But when running this function I get an error in the console Uncaught ReferenceError: _ is not defined I have tried removing the _. in front of debounce but it says debounce is not defined either.

Share Improve this question edited Jul 13, 2022 at 23:02 tony19 139k23 gold badges277 silver badges347 bronze badges asked Mar 15, 2017 at 15:51 twiggtwigg 3,99314 gold badges59 silver badges102 bronze badges 1
  • 2 _ is underscore or lodash, an external library, not Vue. – Bert Commented Mar 15, 2017 at 15:56
Add a ment  | 

1 Answer 1

Reset to default 6

In the example, VueJS use the debounce function from external library like underscoreJS or lodash.

To works with it, you just include this in your file (after install this in your npm modules) like this :

import _ from 'lodash';

new Vue({
    // ...
    methods: {
        // Get the data needed for this page
        fetchData: _.debounce(function () {
            this.$http.get('widgets/quickfindordernumber/' + this.quickFindOrderNumber).then(function (response) {
                console.log(response.body)
            }, function (error) {
                console.log(error);
            });
        }, 500)
    }
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742416788a4439896.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信