【发布时间】:2018-03-29 22:54:39
【问题描述】:
我正在添加我所有的 .js 和 .css 文件以像这样进行预编译
Rails.application.config.assets.precompile = ['*.js', 'application.scss', '*.scss', '*.css.erb']
并在这样的布局head部分中包含application.js
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
即使某些, js 文件在我看来不可用。我在那个目录中有一个名为image-picker.js 的文件。这使得.imagepicker() 函数在我的视图中可用,但它会抛出这样的错误
Uncaught TypeError: $(...).imagepicker is not a function
但是,当我专门使用 <%= javascript_include_tag "image-picker" %>
.js 文件时
它按预期工作。为什么会这样。我需要在配置中设置其他任何东西吗?如何避免在每个页面视图中包含我的资产??
更新
// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, or any plugin's
// vendor/assets/javascripts directory can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file. JavaScript code in this file should be added after the last require_* statement.
//
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//= require jquery
//= require jquery_ujs
//= require jquery-ui
//= require image-picker.js #tried but not working.
//= require_tree .
更新2
我正在使用 jquery-rails - 4.3.1 和 jquery-ui-rails 6.0.1
将jquery-2.1.4.min.js 添加到我的资产中。但是当我使用 //= require jquery 时,我认为它不包括 jquery 到应用程序。
当我尝试手动添加时,一切正常,
//= require jquery
//= require jquery_ujs
//= require jquery-ui
//= require jquery-2.1.4.min #now everything working fine. it seems a dependency problem.
//= require image-picker.js #tried but not working.
//= require_tree .
我想知道这是什么原因。谢谢。
您可以找到非常相似的问题 - here
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-5 asset-pipeline