Vue 解決在element中使用$notify在提示信息中換行問(wèn)題
在項(xiàng)目開(kāi)發(fā)過(guò)程中,遇到如下用戶體驗(yàn)提升需求:需要實(shí)現(xiàn)錯(cuò)誤提示時(shí)根據(jù)后臺(tái)返回錯(cuò)誤列表信息,換行展示。
實(shí)現(xiàn)方式如下:
通過(guò)F12元素查看,在對(duì)應(yīng)的樣式中加入white-space:pre-wrap,該樣式的主要作用是識(shí)別字符串中的換行符'n',故需要在待展示的信息字符串中加入相應(yīng)的換行標(biāo)識(shí)符。
在$notify消息提示中,作用于el-notification:
.el-notification {white-space:pre-wrap !important; }
有的童鞋可能試過(guò)樣式white-space:pre,此時(shí)會(huì)出現(xiàn)的若提示信息內(nèi)容較長(zhǎng)時(shí)顯示不齊全的問(wèn)題。
即使使用自動(dòng)換行樣式(也無(wú)效):
/*設(shè)置內(nèi)容超出后自動(dòng)換行*/word-wrap: break-word;word-break: break-all;
具體區(qū)別可參加以下參數(shù)部分。
補(bǔ)充知識(shí):關(guān)于vue ts項(xiàng)目同時(shí)引入element-ui和ant-design,ts報(bào)錯(cuò)不能編譯的解決方法。
vue ts版本同時(shí)引入ant和element不能打包。
Subsequent property declarations must have the same type. Property ‘$confirm’ must be of type ‘(modalOptios: ModalOptions) => ModalConfirm’, but here has type ‘ElMessageBoxShortcutMethod’.
Subsequent property declarations must have the same type. Property ‘$message’ must be of type ‘Message’, but here has type ‘ElMessage’.
通常vue項(xiàng)目只會(huì)用到一個(gè)ui庫(kù),但是往往會(huì)有一些特殊場(chǎng)景一個(gè)ui庫(kù)不滿足我們業(yè)務(wù)場(chǎng)景,我工作中使用到了ant-design-vue(全局引入)和element-ui(按需加載),同時(shí)項(xiàng)目是ts版本。
elemt,ant ts報(bào)錯(cuò)
我搜索了很多的解決方案,都不符合,我發(fā)現(xiàn)它爆錯(cuò)的地方是兩個(gè)的類型描述文件沖突了,這時(shí)候我把node_modules/element-ui/types/message-box.d.ts 和 node_modules/element-ui/types/message.d.ts 相關(guān)地方注釋后再打包果然不報(bào)錯(cuò)了。
既然能通過(guò)注釋的方式解決打包的問(wèn)題,但是我們不能每次都去注釋一次,這時(shí)候馬上想到node的 fs包能幫我友好解決這個(gè)問(wèn)題。
解決方案:
在項(xiàng)目根目錄創(chuàng)建 config文件夾、os.js文件
編寫os.js文件,如下
/** * 這個(gè)文件在這是為了解決同時(shí)引入element-ui / ant-design ts 爆粗哦, * 解決版本把node_modules 相關(guān)文件注釋了 * */let fs = require(’fs’)let path = require(’path’)let src1 = ’../node_modules/element-ui/types/message.d.ts’annotation(src1, ’$message: ElMessage’)let src2 = ’../node_modules/element-ui/types/message-box.d.ts’annotation(src2, ’$confirm: ElMessageBoxShortcutMethod’)function annotation(src, params) { fs.readFile(path.resolve(__dirname, src), ’utf8’, function(err, files) { if (!err && files !== ’’) { let val = params let has = `// ${params}` let start = files.indexOf(val) let start2 = files.indexOf(has) if (start > -1 && start2 === -1) {var result = files.replace(val, has)fs.writeFile( path.resolve(__dirname, src), result, ’utf8’, function(err) { if (err) { console.log(err) } })console.log(params + ’ 注釋成功!’) } else {console.log(’沒(méi)有需要注釋對(duì)或者已經(jīng)注釋了!’) } } else { console.log(params + ’ 沒(méi)有需要注釋對(duì)或者已經(jīng)注釋了或者注釋文件失敗!’ ) } })}
原來(lái)的命令,我們只需要修改build部分
編寫package.json運(yùn)行命令,把我們編寫的os.js加入到運(yùn)行命令
'scripts': { 'build': 'node config/os.js&vue-cli-service build' },
現(xiàn)在運(yùn)行npm run build
大功告成!
以上這篇Vue 解決在element中使用$notify在提示信息中換行問(wèn)題就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 在layer彈出層中通過(guò)ajax返回html拼接字符串填充數(shù)據(jù)的方法2. asp下利用xml打包網(wǎng)站文件3. idea設(shè)置自動(dòng)導(dǎo)入依賴的方法步驟4. HTML iframe標(biāo)簽用法案例詳解5. XML和YAML的使用方法6. 前端面試學(xué)習(xí)中幾個(gè)常見(jiàn)有用的知識(shí)點(diǎn)7. HTML實(shí)現(xiàn)title 屬性換行小技巧8. 解決Android WebView攔截url,視頻播放加載失敗的問(wèn)題9. JS輪播圖的實(shí)現(xiàn)方法210. 基于Vue CSR的微前端實(shí)現(xiàn)方案實(shí)踐
