VueJS 2 Uncaught ReferenceError:_ 未使用 debounce 定义

twi*_*igg 4 javascript vue.js vuejs2

我正在尝试使函数运行 500 毫秒。按照此处的文档进行操作:

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)
    }
Run Code Online (Sandbox Code Playgroud)

但是当运行这个函数时,我在控制台中收到错误,Uncaught ReferenceError: _ is not defined我尝试删除 _. 在 debounce 前面,但它说 debounce 也没有定义。

thr*_*n19 6

在示例中,VueJS 使用debounce外部库(如 underscoreJS 或 lodash)中的函数。

要使用它,您只需将其包含在您的文件中(在您的 npm 模块中安装之后),如下所示:

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)
    }
});
Run Code Online (Sandbox Code Playgroud)