简体   繁体   English

如何为 Vue 和 webpack 注册全局 lodash mixin

[英]How to register a global lodash mixin for Vue and webpack

I want this mixin我想要这个mixin

_.mixin({
  memoizeDebounce: function (func, wait = 0, options = {}) {
    const mem = _.memoize(function () {
      return _.debounce(func, wait, options)
    }, options.resolver)
    return function () {
      mem.apply(this, arguments).apply(this, arguments)
    }
  }
})

To be available everywhere I import lodash.为了随处可用,我导入了 lodash。 How do I do that?我怎么做? I tried to assign it right in my main.js where Vue is initially started, but the mixin didn't make it to the vuex stores that I wanted to use it in.我尝试在 Vue 最初启动的 main.js 中正确分配它,但是 mixin 没有进入我想要使用它的 vuex 商店。

__WEBPACK_IMPORTED_MODULE_12_lodash___default.a.memoizeDebounce is not a function

How do I do this?我该怎么做呢?

I am using webpack 3.12.0 that came with vue-cli.我正在使用 vue-cli 附带的 webpack 3.12.0。

you coud approach it like this:你可以这样处理它:

import * as _ from 'lodash';

const utilsMixin = {
  computed: {
    _: () => _,
  },
};

export default utilsMixin;

And then import it in your file like this:然后将其导入到您的文件中,如下所示:

Vue.mixin(utilsMixin);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM