是否可以导入在js.erb
文件中使用的JS函数?
例如:
// hello_world.js
const helloWorld = () => console.log('hello world');
export default helloWorld;
和:
// our_file.js.erb
import helloWorld from 'path/to/hello_world.js';
helloWorld();
我有一个流行声,无法发射它,所以假定答案是否的 - 如果确实如此,会很乐意接受一个单词的答案。
如果不是,那么如何工作?事先感谢所有帮助/建设性的批评/嘲弄。
编辑
我不确定建议的重复是的重复 - 我专门想导入功能,而不是将整个脚本加载到DOM中。这可能吗?
通过在窗口上提供可用的功能,您可以在视图或js.erb
文件中调用它。
// hello_world.js
const helloWorld = () => console.log('hello world');
window.helloWorld = helloWorld;
// our_file.js.erb
helloWorld();
我不确定这是否是最佳实践,但这是解决方法。