问题记录:
制作微信小程序有关html文本内容时使用了rich-text
组件,渲染html时有报错: RangeError: Maximum call stack size exceeded
原因:可能html包含不识别的代码
一、 代码记录(名称乱写的)
// xxx.wxml
<view class="www" wx:if="{{qqqq}}" >
<rich-text nodes="{{qqqq}}"></rich-text>
</view>
//xxx.js
onLoad: function (options) {
this.setData({
zzzzzzz:ooo.ppp
}),
getBBB(111).then(res=>{
if (res.code == 200) {
const ttStr = res.data.iii
this.setData({
ttContents: ttStr
})
}
console.log()
})
},
// api.js
export function getBBB(id) {
return request.get('xxx/xxx/xxx', { id: id })
}
二、解决方法
在js文件内加替换
.replace(/style="[^"]+"/gi, “”)
.replace(/style=’[^’]+’/gi, “”) //小程序展示要自己统一的样式,替换了行内样式
.replace(/( )/gi, “”) //替换了注释的内容这能替掉大部分
.replace(/<xml[^>]>(.|\n)</xml>/gi, “”) //替换了xml标签word粘贴带出一堆标签包含在xml里干掉它
.replace(/undefined/gi, “”) //里面莫名有个undefined 干掉
变成这样就解决了,(其他搜的出来的问题以及解决方法暂时在我这个错误里没用到)
//xxx.js
onLoad: function (options) {
this.setData({
zzzzzzz:ooo.ppp
}),
getBBB(111).then(res=>{
if (res.code == 200) {
const ttStr = res.data.iii.replace(/style="[^"]+"/gi, "")
.replace(/style='[^']+'/gi, "") //小程序展示要自己统一的样式,替换了行内样式
.replace(/(<!--(.|[\r\n])*?-->)/gi, "") //替换了注释的内容这能替掉大部分
.replace(/<xml[^>]*>(.|\n)*<\/xml>/gi, "") //替换了xml标签<xml></xml>word粘贴带出一堆标签包含在xml里干掉它
.replace(/undefined/gi, "") //里面莫名有个undefined 干掉
this.setData({
ttContents: ttStr
})
}
console.log()
})
},