2

I have a component that takes an image url as parameter. Within my component I use this url as an image path

<img :src="imgUrl" /> 

I consume this component using

<MyComponent imgUrl="@/assets/images/logo.png" />

When passing this parameter the image file is not found. I tried multiple ways and provide an example

https://codesandbox.io/s/6zmj1q5ypr

What needs to get fixed?

I don't know if this is important but I created my app with the Vue CLI so I use webpack

1 Answer 1

2

Try importing in in your script and setting it as a data property then passing the data property in as the imgUrl.

<template>
  <div id="app">
    <my-component :imgUrl="image" />
    <router-view />
  </div>
</template>

<script>
import MyComponent from '@/components/MyComponent.vue'
import image from '@/assets/logo.png'

export default {
  name: 'App',
  components: {
    MyComponent
  },
  data() {
    return { image }
  }
}
</script>

You'll need to bind it to the property so make sure to precede imgUrl with :.

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

4 Comments

import image from '@/assets/logo.png' will not work as I know. the image can't be included with JS file ,except the base64 trick.
@MosheL, webpack will handle this on compile. Please clone THIS REPO and follow README.md instructions. Thanks
You saved my day. I did not find any other way to return images as parameters in data() and letting vue to check the assets (like require did in 2.x)
@avtomaton glad my answer helped you out. Maybe consider throwing me an upvote ;)

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.