Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Do You Prevent Raw HTML from Rendering in Vue.js

I'm trying to get my Vue.js app to render raw html instead of the code itself.

The code from the server looks like this:

<a href="http://google.com">Google</a>

But when it gets render, the html code get interpreted as text instead of code

<a href="http://google.com">Google</a>

I've tried using v-html, decodeURIComponent, and a filter but no luck:

Is it possible to fetch escaped code from the database and render it as raw html in Vue.js?

like image 355
Sammy32 Avatar asked Dec 17 '25 11:12

Sammy32


1 Answers

Pass it inside a v-html

<span v-html="here_your_html"></span>

update

I see your problem is about escaped content.. Try with a method: unescape(your_html);

like:

<span v-html="unescape(here_your_html)"></span>

and in your methods:

unescaunescapepeS(string) {

  //create an element with that html
  var e = document.createElement("textarea");
   
  //get the html from the created element
  e.innerHTML = string;

  return e.childNodes.length === 0 ? "" : e.childNodes[0].nodeValue;
}

Here a sanbox: https://codesandbox.io/s/hopeful-curran-116fm?file=/src/App.vue

Hope this helps!

like image 116
Raffobaffo Avatar answered Dec 20 '25 06:12

Raffobaffo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!