rails如何在js erb模板中使用webpack加载js
How to use rails webpack loaded js in js erb template
我在 rails 5.0 应用程序中使用 webpacker gem,但我无法在 js.erb I 中获得 javascript 到 execute/be '遇到验证错误。我确定我在这里违反了一些简单的前提,但找不到答案并且不存在编译或控制台错误。我的 application.html.erb
中确实有 <%= javascript_pack_tag 'application' %>
设置如下:
app/javascript/packs/application.js:
import * as CustomerSession from 'customer_sessions';
console.log('Hello World from Webpacker');
app/javascript/customer_sessions/index.js:
export { univgwTabs } from './univgwTabs';
app/javascript/customer_sessions/univgwTabs:
export let univgwTabs = () => {
console.log('hi');
};
js erb 模板调用表单验证错误:
$("#right_side_right_bottom_target").html("<%= j render partial: 'generic_object_new' %>");
CustomerSession.univgwTabs();
Global.resizeWindow();
如果 console.log(CustomerSession)
在您导入它的位置没有问题,那么我发现的最简单的解决方法是将导入的变量分配为全局 windows 属性之一。
// in packs/application.js (or wherever your pack is)
// import
import * as CustomerSession from 'customer_sessions';
// assign
window.CustomerSession = CustomerSession
假设 webpacker 没有问题,您现在应该可以在控制台或 js.erb
文件中 console.log(CustomerSession)
。
我在 rails 5.0 应用程序中使用 webpacker gem,但我无法在 js.erb I 中获得 javascript 到 execute/be '遇到验证错误。我确定我在这里违反了一些简单的前提,但找不到答案并且不存在编译或控制台错误。我的 application.html.erb
中确实有<%= javascript_pack_tag 'application' %>
设置如下:
app/javascript/packs/application.js:
import * as CustomerSession from 'customer_sessions';
console.log('Hello World from Webpacker');
app/javascript/customer_sessions/index.js:
export { univgwTabs } from './univgwTabs';
app/javascript/customer_sessions/univgwTabs:
export let univgwTabs = () => {
console.log('hi');
};
js erb 模板调用表单验证错误:
$("#right_side_right_bottom_target").html("<%= j render partial: 'generic_object_new' %>");
CustomerSession.univgwTabs();
Global.resizeWindow();
如果 console.log(CustomerSession)
在您导入它的位置没有问题,那么我发现的最简单的解决方法是将导入的变量分配为全局 windows 属性之一。
// in packs/application.js (or wherever your pack is)
// import
import * as CustomerSession from 'customer_sessions';
// assign
window.CustomerSession = CustomerSession
假设 webpacker 没有问题,您现在应该可以在控制台或 js.erb
文件中 console.log(CustomerSession)
。