前端vue获取后端返回的富文本内容不是标签是转义字符解决方法

新手的我就直接上代码

template部分(UI框架用的vant)

复制代码
<template>
  <div class="common-bg">
    <van-nav-bar title="消息中心" left-arrow :fixed="true" :placeholder="true" @click-left="onClickLeft" />
    <van-cell v-for="item in list" :key="item.id">
      <div>消息:{{ item.title }}</div>
      <div>时间:{{ item.createTime }}</div>
      <div v-html="change(item.content)"></div>
    </van-cell>
  </div>
</template>
复制代码

script部分

复制代码
data() {
    return {
      list: []
   }
methods: {
    initData() {
      getMessageList().then((res) => {
        console.log(res, '111');
        this.list = res.data
      })
    },
    onClickLeft() {
      this.$router.go(-1)
    },
    change(strValue) {
      console.log(strValue,'1');
      strValue = strValue.replace(/&amp;/g, "&");
      strValue = strValue.replace(/&lt;/g, "<");
      strValue = strValue.replace(/&gt;/g, ">");
      strValue = strValue.replace(/&quot;/g, "\"");
      strValue = strValue.replace(/&#39;/g, "'");
      console.log(strValue,'2');
      return strValue;
    }
  }
复制代码

实现效果

 

缺点在于如果还有需要替换别的转义字符的话,得自行添加,如果还有什么好方法,欢迎提醒

来源https://www.freesion.com/article/19891451326/

posted @   南城无秋  阅读(970)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具

阅读目录(Content)

此页目录为空

欢迎这位怪蜀黍来到《前端vue获取后端返回的富文本内容不是标签是转义字符解决方法 - 南城无秋 - 博客园》
点击右上角即可分享
微信分享提示