前言
使用uni-app開發(fā)微信小程序時(shí),使用text顯示字符串(字符串帶\n),需要在\n處直接換行。
1、本地字符串,可以直接換行顯示。
2、后臺(tái)返回字符串,直接換行失效。原因:渲染時(shí) \n 直接被當(dāng)成字符串處理了,根本不識(shí)別。
效果圖
? ???
實(shí)現(xiàn)
1、使用replace()方法,字符串內(nèi)的 \n 做一次替換。
具體演示代碼
1、使用后臺(tái)返回字符串
<!-- 替換第一個(gè)\n -->
<text>{{ name.replace(/\\n/,'\n') }}</text>
<!-- 全部替換 -->
<text>{{ name.replace(/\\n/g,'\n') }}</text>
2、使用本地字符串
<template>
<view>
<text>{{ name }}</text>
</view>
</template>
<script>
export default {
data() {
return {
name: '周二至周五 10:00-19:00\n周六至周日 09:00-19:00 \n周一閉館'
}
}
}
</script>
PS:
var str = "namehagna".replace("a",""); 則只會(huì)替換第一個(gè)a
var str = "namehagna".replace(/a/g,""); 會(huì)替換所有: /g 代表匹配全文文章來源:http://www.zghlxwxcb.cn/news/detail-606619.html
使用這種方法,只能在text標(biāo)簽中生效,直接在view標(biāo)簽中使用這種方法不生效文章來源地址http://www.zghlxwxcb.cn/news/detail-606619.html
到了這里,關(guān)于uni-app開發(fā)微信小程序數(shù)據(jù) \n 換行符失效問題的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!