Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VueJS 2 Uncaught ReferenceError: _ is not defined with debounce

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

https://v2.vuejs.org/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.

like image 322
twigg Avatar asked Oct 31 '25 20:10

twigg


1 Answers

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)
    }
});
like image 115
throrin19 Avatar answered Nov 02 '25 11:11

throrin19



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!