简体   繁体   中英

Rendering escaped HTML from api call with vue.js

I'm trying to render out data returned from an api call with Vue.

One portion of the data is coming in the form of escaped html like so:

body: "<ul> ↵ <li>This is <strong>Test 
Text.</strong></li> ↵</ul>"

I have tried the v-html directive as follows:

      <div  v-for="(item, index) in filteredList"> 
         <div v-html="item.body"></div>
      </div>

It's rendering out but but with visible anchor tags, exactly as it looks below, ul, li, strong tags all visible in the browser

  <ul> <li>This is &nbsp;<strong>Test Text</strong></li> </ul>

I've tried a decoding function (which rendered it exactly the same as the v-html is), a sanitizing plug-in, looked over this post Vue template - convert HTML special characters (numberic) to symbols? and many others but I just can't seem to get it to render properly

You could try using the DOMParser .

 const doc = new DOMParser().parseFromString("&lt;li&gt;", "text/html"); const textContent = doc.documentElement.textContent; console.log(textContent); 

After running it through here you can then pass the results to v-html .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM