移动端 [Vue warn]: Error in render: "TypeError: _v$label.replaceAll is not a function",h5没问题,移动端报错。

核心原因:移动端环境不支持 replaceAll 方法

String.prototype.replaceAll 是 ES2021 新增的方法(2021 年标准化),虽然现代浏览器(如 Chrome 85+、Safari 13.1+)已支持,但部分移动端环境可能存在兼容问题:

  • 老旧的 Android 系统 WebView(如 Android 7.0 及以下的系统自带浏览器内核)
  • 某些小程序的基础库版本过低(未实现该方法)
  • 原生 App 内嵌的 WebView 内核版本较旧

需要改成replace函数,示例:

v.label_all = v.label?.replaceAll(this.popupSearchText,`<text class="red">`+this.popupSearchText+'</text>')

改成:

v.label_all =  v.label?.replace(new RegExp(this.popupSearchText, 'g'), `<text class="red">${this.popupSearchText}</text>`);

Logo

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐