0

I'm trying to interpolate a value of photo within Vue. Here is the components:

Explaination: We are defining a data set as null. We are then, performing a post method to a backend database where we get photo,email,name and id. Everything works fine until the photo value. As you can see I performed a slice method on the string which removed unnecessary data. The output of this is just the file name.

<script>

export default {
  data() {
    return {
      photo: null,
      email: null,
      name: null,
      id: null
    };
  },
 created() {
this.axios
      .post("http://127.0.0.1:8000/api/auth/me", "body", {
        headers: axiosHeader
      })
      .then(response => {
        console.log(response.data);
        this.name = response.data.name;
        this.email = response.data.email;
        this.id = response.data.id;
        this.photo = "@/photodatabase/" + response.data.photo.slice(37, 56);
        console.log(this.photo);
      })
      .catch(error => {
        console.log(error);
      });
  }
};
</script>

The value I got is @/photodatabase/041120_09_24_03.jpg when I console.log(this.photo) which is the correct value. However when I try to interpolate it like:

<img v-else-if="photo != null" :src="photo"
  height="200px" width="200px" />

The image doesn't show. When I inspected element, you could see on the img tag that the value was @/photodatabase/041120_09_24_03.jpg which was the correct value.

I tried interpolating like this:

<img v-else-if="photo != null" :src="`${photo}`"     
    height="200px" width="200px" />

and it still doesn't work. However when I didn't interpolate the value of photo, didn't use the v-bind shorthand : and just copied and pasted the value of photo to the src prop like this:

<img
          v-else-if="photo != null"
          src="@/photodatabase/041120_09_24_03.jpg"
          height="200px"
          width="200px"
        />

Then it works. What am I missing here?

2
  • 1
    Can you show the corresponding v-if? Commented Nov 4, 2020 at 9:54
  • @Pacholoamit! If my answer works for you and you are happy - then in general you would accept the answer. This will not only help other people with the same issue but it will also mean people are more likely to help you in the future with any other issues you have. You can read about accepting here: stackoverflow.com/help/accepted-answer Commented Nov 14, 2020 at 5:51

3 Answers 3

1

Because is dynamic image src must photo be absolute URL Like: http://127.0.0.1:8000/photodatabase/041120_09_24_03.jpg or

if in the same server

/photodatabase/041120_09_24_03.jpg

or you can easily send absolute URL on response.

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

Comments

0

I believe that this isn't the correct URL @/photodatabase/041120_09_24_03.jpg

You've wrote "it still doesn't work" so You get "404 Not found" error?

That link should lead to Your backend (static asset or Laravel controller). @ is rewritten by Webpack during compilation time to Your public url - it won't work as a dynamic path in runtime...

This code

<img src="@/photodatabase/041120_09_24_03.jpg" />

...should become something else after compilation. Inspect it. It could be something like that:

<img src="/static/photodatabase/041120_09_24_03.jpg" />

So if You didn't map @ in vue-router, nor in Laravels routes, then You should change @ in that link. You can try simply /photodatabase/041120_09_24_03.jpg

4 Comments

Here's picture if I inspect element I got no problem in the console as well. No errors.
I did a variety of checks. There's nothing wrong with the controller since when I did the same request via postman it works. It has something to do with the syntax I think or v-bind. Because if I don't v-bind it, and I just copy paste the link to the src prop directly. It renders.
And what URL did You use Postman? Because @/photodatabase/041120_09_24_03.jpg is just a tailing part, and I think the problem is with the beginning that cant be resolved by router...?
No, you were right in a way. It had something to do with webpack. I fixed it with the require() command. hahah. Thanks for pointing me in the right direction.
0

Try using the require handler

<img v-else-if="photo != null" :src="require(${photo})"     
    height="200px" width="200px" />

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.