解决数据库存储富文本信息可能会出现转义等bug

问题

存储富文本信息时,直接存储可能会出现标签转义等bug导致页面数据渲染失败

解决办法

利用encodeURIComponent和decodeURIComponent以及atob和btoa进行编码和解码

代码

// 将富文本解码
function handleDetail(){
  content.value = window.decodeURIComponent(atob(data.content as string));
}
// 将富文本编码
function handleAdd(){
  formData.content = btoa(window.encodeURIComponent(content.value));
}

 

posted @ 2024-05-21 13:38  南无、  阅读(115)  评论(0)    收藏  举报