6

In my case, I am replacing a link on top of paragraph element using v-html

Please find the code snippet as follows

 <p v-html="websiteHTML"></p>

where websiteHTML contains: <a v-bind:href="google.com/">Google</a>

The <p> tag is being rendered with Google but doesn't have hyperlink to navigate to https://www.google.com/

Could you please help in finding the error?

2
  • 4
    I corrected some formatting problems with the question but there are others that aren't recoverable -- so this may just be a problem with the question text: does websiteHTML simply contain the string "Google"? Or does it contain something like <a href="https://google.com">Google</a>? If you don't tell Vue to include the link, it won't. Commented Apr 13, 2019 at 14:05
  • Thank Daniel for your response... app.js -> websiteHTML: '<a v-bind:href="google.com/">Google</a>' index.html -> <p v-html="websiteHTML"></p> Commented Apr 14, 2019 at 15:42

3 Answers 3

7

The HTML string you include in your variable should just be HTML, not Vue template code. When you tried including vue template directives, the framework wrote the anchor tag into the DOM with the literal attribute "v-bind:href" instead of the desired "href":

new Vue({
  el: '#app',
  data: {
    websiteHTMLNo: '<a v-bind:href="https://google.com/">Google</a>', // <-- won't work
    websiteHTMLYes: '<a href="https://google.com/">Google</a>'  // <-- do this instead
  },
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <p v-html="websiteHTMLNo"></p>
  <p v-html="websiteHTMLYes"></p>
</div>

(If you actually do need to inject template code instead of plain HTML, you need to use Vue.compile instead of v-html to parse it.)

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

1 Comment

Thank you Soo much Daniel for the help and providing solution. Much appreciated.. Thanks for the vue.compile concept
0

if you want to pass a var inside your link.

const url: string = 'https://google.fr/'
const html: string = `<a href="${url}">Google</a>`
<p v-html="html"></p>

Comments

-1

I had a similar problem with v-html.

In my case, I received an array of objects in JSON format from an API, although I did JSON.Parse(object), I couldn't receive the data and after a lot of head-scratching, I discovered that it was the name of the properties of my object that in Json was returned in lowercase and in my object it started in uppercase...

work... export default interface Info { prio: Prio expires?: Date | null html: string }

don't work... export default interface Info { Prio: Prio Expires?: Date | null Html: string }

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.