【问题标题】:VueJS 2 Uncaught ReferenceError: _ is not defined with debounceVueJS 2 Uncaught ReferenceError: _ is not defined with debounce
【发布时间】:2017-08-06 11:34:31
【问题描述】:

我正在尝试消除一个运行 500 毫秒的函数。按照此处的文档:

https://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)
    }

但是在运行此函数时,我在控制台中出现错误Uncaught ReferenceError: _ is not defined 我已尝试删除_。在 debounce 前面,但它说 debounce 也没有定义。

【问题讨论】:

  • _ 是下划线或 lodash,一个外部库,而不是 Vue。

标签: javascript vue.js vuejs2


【解决方案1】:

在示例中,VueJS 使用来自 underscoreJS 或 lodash 等外部库的 debounce 函数。

要使用它,你只需将它包含在你的文件中(在你的 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)
    }
});

【讨论】:

    猜你喜欢
    • 2017-04-09
    • 2019-07-24
    • 1970-01-01
    • 2020-03-25
    • 2017-02-25
    • 1970-01-01
    • 2017-06-07
    • 2015-12-24
    • 1970-01-01
    相关资源
    最近更新 更多