-
Notifications
You must be signed in to change notification settings - Fork 920
Closed
Description
Hey there,
I'm new to vue (which I love so far), just setting up an example project.
Hoping someone can tell me where I'm going wrong with aliasing requires.
Using the vue-cli webpack template for example, changing App.vue template to
<template>
<div id="app">
<img class="logo" src="assets/logo.png">
...
<script>
import Hello from 'components/Hello'
and webpack.base.conf.js
alias: {
'src': path.resolve(__dirname, '../src'),
'assets': path.resolve(__dirname, '../src/assets'),
'components': path.resolve(__dirname, '../src/components')
}
Then moving App.vue into the folder src/App/
as index.vue webpack.
npm run build
gives:
ERROR in ./~/vue-html-loader!./~/vue-loader/lib/selector.js?type=template&index=0!./src/App/index.vue
Module not found: Error: Cannot resolve 'file' or 'directory' ./assets/logo.png in /Users/Alex/Work/testing/quick/src/App
@ ./~/vue-html-loader!./~/vue-loader/lib/selector.js?type=template&index=0!./src/App/index.vue 1:55-83
Not sure what is going on here the image seems to be taken care of by url-loader as its encoded into the js. Which I think differentiates it from this issue #172 which Evan already replied to.
czerasz, weisk, happygrizzly, fengerzh, zhanglonggang001 and 2 more
Activity
[-]html image src not require not respecting webpack aliases?[/-][+]html image src require not respecting webpack aliases?[/+]yyx990803 commentedon Apr 6, 2016
vue-html-loader
andcss-loader
translates non-root URLs to relative paths. In order to treat it like a module path, prefix it with~
:k1sul1 commentedon Aug 29, 2016
Not directly related, but if you're using the vue-cli webpack boilerplate and webpack is giving you pain when trying to bind images from the default data object (or whatever this is called, my first Vue project)..:
<img :src="img">
will result in you scratching your head and wondering what you did wrong. Webpack "has no means of rewriting it" and it doesn't work. Wrap it inrequire()
and if Airbnb ESLint gives you trouble, turnglobal-require
to 0 in .eslintrc.js.vuejs-templates/webpack#126
PrimozRome commentedon Sep 27, 2016
@k1sul1 I tried this solution but I am getting error
Uncaught Error: Cannot find module '../assets/img/profiles/6.jpg'
. Any idea?k1sul1 commentedon Sep 27, 2016
Well, if you actually read the error message, you might actually figure it out :)
It clearly says "Cannot find module", so your path is probably broken. Meaning the file doesn't exist in that location :)
PrimozRome commentedon Sep 27, 2016
@k1sul1 ok please don't think I am posting this before checking something like that... Ofcourse it exists... For example this is working:
<img src="../assets/img/profiles/6.jpg">
while this is not with the error I posted above
k1sul1 commentedon Sep 27, 2016
Interesting. Can't say I have any ideas other than adding
return
to the function, but I'm assuming you have that in your actual code. I'm not an expert in any of these things so I can't really help you.Could be a platform issue, are you using Windows perhaps?
For me,
require
just worked straight away.PrimozRome commentedon Sep 27, 2016
@k1sul1 tried that too when I noticed I don't have one, but without any luck. We are discussing this on the Vue.js forum as well but no success right now http://forum.vuejs.org/topic/5343/webpack-serve-dynamic-images/15.
ghost commentedon Feb 6, 2017
thx~
oswaldofreitas commentedon Apr 17, 2017
this code...
is returning:
oswaldofreitas commentedon Apr 17, 2017
no more, changed to
img.logo(src='~@/assets/logo.png')
and workedcodeofsumit commentedon May 3, 2017
thanks @oswaldomilet
worked for me. Very weird...
https://github.com/vuejs/vue-loader/issues/193
12 remaining items
jofftiquez commentedon Jul 21, 2017
This
@/
also works.bcowell commentedon Jul 21, 2017
@jofftiquez weird, I'm trying the same thing and getting an error.
ERROR Failed to compile with 1 errors
This dependency was not found:
proYang commentedon Aug 11, 2017
why use "~", it will work. I want to kown the reason
oswaldofreitas commentedon Aug 11, 2017
@proYang Please see the docs:
helmerdx commentedon Apr 27, 2018
@oswaldofreitas you are the man 👍
Sangeethks commentedon May 9, 2018
If you are using data property to assign image to src, then use require(<image_path>). Like,
<img :src="image">
data() { return { image: require('@/assets/icons/profile.png') } }
jofftiquez commentedon Oct 25, 2018
Another solution that I am using is
<img :src="require('image/path/here')">
motivis commentedon Nov 29, 2018
does anyone know how to npm install the package in order to use REQUIRE in the client application ?
I forgot what package that is
brianwachira commentedon Jan 28, 2019
worked for me too.
src="~@/assets/image.png"