简体   繁体   English

如何使用npm将jquery添加到phoenix 1.3

[英]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.6phoenix 1.3node 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.jsassets/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.

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