溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Vue 項(xiàng)目中element使用$notify出現(xiàn)換行如何解決

發(fā)布時間:2020-11-12 15:19:45 來源:億速云 閱讀:1050 作者:Leah 欄目:開發(fā)技術(shù)

今天就跟大家聊聊有關(guān)Vue 項(xiàng)目中element使用$notify出現(xiàn)換行如何解決,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

在項(xiàng)目開發(fā)過程中,遇到如下用戶體驗(yàn)提升需求:需要實(shí)現(xiàn)錯誤提示時根據(jù)后臺返回錯誤列表信息,換行展示。

實(shí)現(xiàn)方式如下:

通過F12元素查看,在對應(yīng)的樣式中加入white-space:pre-wrap,該樣式的主要作用是識別字符串中的換行符"\n",故需要在待展示的信息字符串中加入相應(yīng)的換行標(biāo)識符。

在$notify消息提示中,作用于el-notification:

.el-notification {white-space:pre-wrap !important; }

有的童鞋可能試過樣式white-space:pre,此時會出現(xiàn)的若提示信息內(nèi)容較長時顯示不齊全的問題。

即使使用自動換行樣式(也無效):

/*設(shè)置內(nèi)容超出后自動換行*/
word-wrap: break-word;
word-break: break-all;

具體區(qū)別可參加以下參數(shù)部分。

Vue 項(xiàng)目中element使用$notify出現(xiàn)換行如何解決

Vue 項(xiàng)目中element使用$notify出現(xiàn)換行如何解決

Vue 項(xiàng)目中element使用$notify出現(xiàn)換行如何解決

補(bǔ)充知識:關(guān)于vue ts項(xiàng)目同時引入element-ui和ant-design,ts報錯不能編譯的解決方法。

vue ts版本同時引入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)目只會用到一個ui庫,但是往往會有一些特殊場景一個ui庫不滿足我們業(yè)務(wù)場景,我工作中使用到了ant-design-vue(全局引入)和element-ui(按需加載),同時項(xiàng)目是ts版本。

Vue 項(xiàng)目中element使用$notify出現(xiàn)換行如何解決

elemt,ant ts報錯

我搜索了很多的解決方案,都不符合,我發(fā)現(xiàn)它爆錯的地方是兩個的類型描述文件沖突了,這時候我把node_modules/element-ui/types/message-box.d.ts 和 node_modules/element-ui/types/message.d.ts 相關(guān)地方注釋后再打包果然不報錯了。

Vue 項(xiàng)目中element使用$notify出現(xiàn)換行如何解決

Vue 項(xiàng)目中element使用$notify出現(xiàn)換行如何解決

既然能通過注釋的方式解決打包的問題,但是我們不能每次都去注釋一次,這時候馬上想到node的 fs包能幫我友好解決這個問題。

解決方案:

在項(xiàng)目根目錄創(chuàng)建 config文件夾、os.js文件

Vue 項(xiàng)目中element使用$notify出現(xiàn)換行如何解決

編寫os.js文件,如下

/**
 * 這個文件在這是為了解決同時引入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('沒有需要注釋對或者已經(jīng)注釋了!')
      }
    } else {
      console.log(
        params + ' 沒有需要注釋對或者已經(jīng)注釋了或者注釋文件失敗!'
      )
    }
  })
}

原來的命令,我們只需要修改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 項(xiàng)目中element使用$notify出現(xiàn)換行如何解決

看完上述內(nèi)容,你們對Vue 項(xiàng)目中element使用$notify出現(xiàn)換行如何解決有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI