【发布时间】:2018-09-11 03:12:21
【问题描述】:
我目前正在尝试使用 webpack.ProvidePlugin,但似乎没有正确加载它。这是我的environment.js
var webpack = require("webpack");
const { environment } = require("@rails/webpacker");
const vueLoader = require("./loaders/vue");
environment.loaders.append("vue", vueLoader);
environment.plugins.append(
"Provide", // arbitrary name
new webpack.ProvidePlugin({
Vue: "vue",
_: "lodash"
})
);
module.exports = environment;
还有我的文件Header.vue
<h1>{{_.capitalize(title)}} </h1>
错误:
vue.runtime.esm.js:587 [Vue warn]: Property or method "_" is not defined on
the instance but referenced during render. Make sure that this property is
reactive, either in the data option, or for class-based components, by
initializing the property.
【问题讨论】:
-
题外话:无需添加vue loader。它默认出现。查看我自己的代码,唯一的区别是您有“Provide”的第一个参数,我有“ProvidePlugin”。不知道这是否有区别。其次,为什么不直接在 vue 文件中导入 lodash 方法呢?可能问题是在编译 vue 文件时,webpack 看不到全局上下文。此外,根据 Vue 的常见做法,您应该在计算函数中进行大写
-
嗯,使用
ProvidePlugin的全部意义在于,您可以在全局范围内提供这些值,我没有导入 _ 的问题,除了不方便,某些常用模块不应该到处都是回购。 -
不,我的意思是只导入您使用的那些函数。然后,当 babel 为导入函数实现良好的 tree-shaking 算法时,你就不必重写任何东西了。但这只是一个建议。
-
@Kkulikovskis 我正在为其他事情做,而不是像 lodash 之类的事情
标签: ruby-on-rails webpack webpacker