1

I have a vue page which uses v-html to place an html content inside a <div> as follows:

<div v-html="noDataMessage">               
</div>

And the data of noDataMessage is set in created() lifecycle hook of vue.

created() {  
   this.noDataMessage = "<img src='../assets/Content/img/Myfav_empty.png' width='35px' height='35px'>";
}

The html tag is showing properly, but it is not able to fetch the image. I tried to load the image separately to see if image path is correct, and got the image.

Am I missing anything particular to vue.js or v-html in particular?

3
  • If you look at this fiddle it seems to work fine. I think the issue is in fact in your path. Edit: Perhaps take a look at this link which might solve your issue Commented Dec 5, 2018 at 8:35
  • Look in your browser console's network tab to see if the request for the image is being made or not, and if it is, whether it's failing due to a 404 error, in which case, as the previous commenter suggested, your path would be wrong Commented Dec 5, 2018 at 8:37
  • Instead of this path ../assets/Content/img/Myfav_empty.png, try this one @/assets/Content/img/Myfav_empty.png, or this one ~@/assets/Content/img/Myfav_empty.png Commented Dec 5, 2018 at 9:22

2 Answers 2

3

The issue is in the src url you are using, the src url root is the public directory.

For example, if you have all your images in public->images directory then your code will look like this:

this.noDataMessage = "<img src='images/Myfav_empty.png' width='35px' height='35px'>";
Sign up to request clarification or add additional context in comments.

2 Comments

This has solved the issue. But I wonder, what would someone do if relative path is to be specified?
Could you tell me an example to the relative path you want, because if the relative path is outside the public directory then it won't work with this way.
0

It seems perfect only.

I have implemented the same way to fetch the image, Here I can see the image. Please check the path once again.

You can check below example where I have tried to implement same way :http://jsfiddle.net/suprabhasupi/eywraw8t/491005/

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.