Skip to content
Advertisement

Vue.js/JavaScript – Is it possible to insert a link/anchor inside a text?

Let’s say I have a paragraph with some text that I get from some database:

JavaScript

However, this text may contain some references to other pages in my aplication:

JavaScript

So I would like these references to get turned into links to said pages:

JavaScript

I tried using the replace function in the script like this:

JavaScript

But the characters all get escaped resulting in the anchor html code getting shown on the page.

Is there a way to stop the characters from being escaped, or even another way to turn [references] in the middle of the text into working links to another page?

Advertisement

Answer

If you don’t want your HTML to be escaped, use the v-html directive.

From the docs:

The double mustaches interprets the data as plain text, not HTML. In order to output real HTML, you will need to use the v-html directive:

Example:

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement