简体   繁体   English

使用vue.js渲染API调用中的转义HTML

[英]Rendering escaped HTML from api call with vue.js

I'm trying to render out data returned from an api call with Vue. 我正在尝试渲染从Vue的api调用返回的数据。

One portion of the data is coming in the form of escaped html like so: 数据的一部分以转义html的形式出现,如下所示:

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

I have tried the v-html directive as follows: 我尝试了如下的v-html指令:

      <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,强标签在浏览器中都可见

  <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? 我尝试过解码功能(使其与v-html完全相同),一个消毒插件,查看了此帖子Vue模板-将HTML特殊字符(数字)转换为符号? and many others but I just can't seem to get it to render properly 和许多其他东西,但我似乎无法正确渲染

You could try using the DOMParser . 您可以尝试使用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 . 在这里运行完之后,您可以将结果传递给v-html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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