[英]How to add jquery to phoenix 1.3 using npm
There are good official phoenix docs explaining how to add an npm dependency (even uses jquery). 有好的官方凤凰文档解释了如何添加npm依赖项(甚至使用jquery)。 Evidently, I am missing something here.
显然,我在这里缺少什么。 Here is what I have:
这是我所拥有的:
Using elixir 1.6
, phoenix 1.3
, and node 8.9
on Mac OSX. 在Mac OSX上使用
elixir 1.6
, phoenix 1.3
和node 8.9
。
assets/js/app.js contains: asset / js / app.js包含:
import $ from 'jquery';
assets/brunch-config.js contains: asset / brunch-config.js包含:
npm: {
enabled: true,
npm: {
globals: {
$: 'jquery',
jQuery: 'jquery'
}
}
}
package.json contains: package.json包含:
"dependencies": {
"jquery": "^3.3.1",
"phoenix": "file:../deps/phoenix",
"phoenix_html": "file:../deps/phoenix_html"
}
My "application logic" - in a js file imported from assets/js/app.js : 我的“应用程序逻辑”-在从asset / js / app.js导入的js文件中:
$(function () {
alert('Hello');
});
Do have a workaround by simply (manually) copying jquery.min.js
from assets/node_modules/query/dist
into assets/vendor
. 通过简单地(手动)将
jquery.min.js
从assets/node_modules/query/dist
复制到assets/vendor
。 But am keen to understand what is going wrong with the automatic inclusion using npm
and brunch asset pipeline. 但是我很想了解使用
npm
和早午餐资产管道进行自动包含的问题所在。
You don't need import $ from 'jquery';
您不需要
import $ from 'jquery';
in app.js 在app.js中
In assets/brunch-config.js
do this instead: 在
assets/brunch-config.js
执行以下操作:
npm: {
enabled: true,
globals: {
$: 'jquery',
jQuery: 'jquery'
}
}
(you have extra npm
section inside top npm
section) (你有额外的
npm
顶部内侧部分npm
部分)
I've answered similar question before, still actual: https://stackoverflow.com/a/43265955/1173020 我之前已经回答过类似的问题,但仍然是实际的: https : //stackoverflow.com/a/43265955/1173020
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.