[英]How to properly import JS and CSS files in Webpacker?
I installed Webpacker gem into my Rails app and have difficulties to import JS and CSS files from assets folder to app/javascript/packs folder.我将 Webpacker gem 安装到我的 Rails 应用程序中,但很难将 JS 和 CSS 文件从资产文件夹导入到 app/javascript/packs 文件夹。
I'm mentioning that app/javascript/packs/application.js looks like this:我提到 app/javascript/packs/application.js 看起来像这样:
/* eslint no-console:0 */
// This file is automatically compiled by Webpack, along with any other files
// present in this directory. You're encouraged to place your actual application logic in
// a relevant structure within app/javascript and only use these pack files to reference
// that code so it'll be compiled.
//
// To reference this file, add <%= javascript_pack_tag 'application' %> to the appropriate
// layout file, like app/views/layouts/application.html.erb
console.log('Hello World from Webpacker')
and it works properly(in console I get this "Hello World.." text.)并且它工作正常(在控制台中我得到这个“Hello World..”文本。)
I tried to copy, for example jquery.slimmenu.js into packs folder and to include it like this:我试图复制,例如jquery.slimmenu.js到packs文件夹中,并像这样包含它:
//= require jquery.slimmenu.js
but it's not finding in console, there is just application.js and bunch of 404 not find JS and CSS files但在控制台中找不到,只有 application.js 和一堆 404 找不到 JS 和 CSS 文件
your main.js file can be in any directory in app/javascript for example, create app/javascript/components then add in your application.js例如,您的 main.js 文件可以在 app/javascript 中的任何目录中,创建 app/javascript/components 然后添加到您的 application.js
import 'jquery'
require("../components/main");
then in your HTML add然后在你的 HTML 添加
<%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
and script from application.js must be loaded必须加载来自 application.js 的脚本
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.