简体   繁体   English

在Rails 5.1中使用Webpacker并加载图像资源

[英]Using Webpacker in Rails 5.1 and loading image assets

So I've migrated my Rails application over to Webpack but I'm having trouble loading image assets. 所以我已经将我的Rails应用程序迁移到Webpack,但是我在加载图像资源时遇到了问题。 In the Webpacker documentation it states that I can load my assets using the following: 在Webpacker 文档中,它声明我可以使用以下内容加载我的资产:

<img src="<%= asset_pack_path 'images/logo.svg' %>" />

But this doesn't seem to be compiled down when I run rails webpacker:compile 但是当我运行rails webpacker:compile时,似乎没有编译下来

My folder structure looks like so 我的文件夹结构看起来像这样

app/javascript:
  ├── packs:
  │   # only webpack entry files here
  │   └── application.js
  └── src:
  │   └── application.css
  └── images:
      └── logo.svg

and my webpacker.yml contains: 我的webpacker.yml包含:

default: &default
  source_path: app/javascript
  source_entry_path: packs
  public_output_path: packs
  cache_path: tmp/cache/webpacker

I'm not great at this new Webpack stuff but I assume it has something to do with my webpacker.yml 我不擅长这个新的Webpack,但我认为它与我的webpacker.yml

Has anyone experienced this before? 有谁之前经历过这个吗? There seems to be little advice on the internet. 互联网似乎没有什么建议。

Ta! TA!

I think I can resolve this by manually importing the images from inside my application.js 我想我可以通过手动从application.js导入图像来解决这个问题

So for example; 所以例如;

# /app/javascript/packs/application.js
import '../images/logo.svg';

Then lets me reference this using the asset_pack_path 然后让我使用asset_pack_path引用它

Still if somebody can tell me a better way i'd really appreciate it. 如果有人能告诉我一个更好的方式,我真的很感激它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM