1

I have a new vue site which was built using vue-cli but my image will not load at all and can't figure out why

 <router-link class="logo" to="/" tag="img" src="@/assets/logo.png" style="cursor: pointer"/>

I have the image saved in the assets folder

enter image description here

enter image description here

console

enter image description here

enter image description here

But if I use the below it does display

<img src="@/assets/logo.png" />

enter image description here

console

enter image description here

3 Answers 3

1

With vue cli images are considered as modules so you should import or require them :

 <router-link :src="require(@/assets/logo.png)" style="cursor: pointer" class="logo" to="/" tag="img" />

Sign up to request clarification or add additional context in comments.

Comments

0

As far as I can see, <router-link> does not have a src prop: https://router.vuejs.org/api/#router-link-props

3 Comments

The router-link is rendered as an HTML img so the src is valid
according to this : Sometimes we want <router-link> to render as another tag, e.g <li>. Then we can use tag prop to specify which tag to render to, and it will still listen to click events for navigation
It matters that router-link doesn't have a src prop: since it doesn't itself know about the src prop it's going to pass the value as-is. It's not like Vue looks at all attributes everywhere to see if they look like paths that should be rewritten.
0

When you use Vue with Webpack (almost always), Vue-loader is used to compile your .vue files. When compiling <template> part of the file, it looks for some common tags like <img> and is able to transform content of the src tags into Webpack module requests

So your <img src="@/assets/logo.png" /> is at compile time transformed (essentially) into <img :src="require('@/assets/logo.png')" />

See the link above for tag/attribute combinations transformed by default. If the combination you need is not there (as in your case), you can either configure Vue-loader (transformAssetUrls) as many popular UI libraries do (for example Vuetify for it's v-img component) or you must do it by hand as @Boussadjra Brahim suggested (with missing quotes tho):

<router-link :src="require('@/assets/logo.png')" to="/" tag="img" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.