javascript - 使用 Rails 5 导入 Webpack
问题描述
我有一个相当大的代码库,我正试图从 Rails 4 迁移到 5,其中主要部分是从资产管道过渡到 webpack 以管理需求和包。
<!DOCTYPE html>
<html>
<% content_for :extra_head_scripts do %>
<% end %>
<head>
<title>Cloverhound</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<%= javascript_pack_tag "application-editor", 'data-turbolinks-track': 'reload' %>
<%= stylesheet_link_tag "application-editor", :media => "all" %>
<%= csrf_meta_tags %>
<%= favicon_link_tag 'favicon.ico' %>
<%= render partial: "layouts/shared/head_scripts" %>
<!-- this javascript_tag passes the user's locale to the editor to enable translations -->
<%= javascript_tag do %>
I18n.defaultLocale = "<%= I18n.default_locale %>";
I18n.locale = "<%= current_user.locale.shortcode.to_s %>";
<% end %>
</head>
<body>
<%= render partial: "shared/svg_sprite" %>
<%= yield %>
</body>
</html>
在上面的文件中,我试图加载 I18n 模块以便可以使用它。
console.log('Hello World from Webpacker');
console.log("Application-Editor");
import Sortable from 'sortablejs';
//var I18n = require('i18n-js');
import I18n from 'i18n';
以上是我的简单 webpack 文件,可以正确登录到 chrome 控制台,但是,I18n 仍然未定义。如果我尝试在 application-editor.js 中调用 I18n,它也会返回 undefined。但是,如果我将 i18n 更改为 i18n-bad 或类似的东西,我会收到一条错误消息,指出找不到该模块。这让我相信我的模块正在被找到,但是在导入它的过程中出现了问题。我对 webpacker 很陌生,所以任何想法都会有所帮助。
解决方案
推荐阅读
- javascript - TypeScript:只返回界面的一部分
- python - 避免对值条形图进行排序
- sumifs - 数组上的 VBA SUMIF
- c++ - 为什么要在 packbits 算法中给出文件大小?
- regex - 从 postgres 表中提取括号之间的文本而不创建其他列
- sql - 处理我的不带子查询的版本请求
- sql-server - 如何使用 SSIS 代理用户从 SSIS 使用 WCF 服务?
- excel - Excel Power Query、VBA 和重新计算:很复杂
- c# - 如何制作带有文件上传的 Asp.Net Core 表单?
- spring - 添加@PathVariable后,资源变得不可访问