Vue.js使用this.$confirm換行顯示提示信息實例
Vue.js使用this.$confirm換行顯示提示信息
在寫一個簡單的按鈕點擊確認框信息的時候,發(fā)現(xiàn)換行不能用\n。用了< br>發(fā)現(xiàn)也是字符串的輸出形式
去查了下發(fā)現(xiàn)需要使用$createElement來創(chuàng)建
這里我需要顯示兩行信息。
代碼如下:
creatNew(){ const h = this.$createElement this.$confirm('提示', { title: '提示', message: h('div', [ h('p', '新建會導(dǎo)致之前設(shè)置失效'), h('p', '是否繼續(xù)新建?') ]), confirmButtonText: '確定', cancelButtonText: '取消' }).then(() => { ....//調(diào)用新建方法 }).catch(()=>({}))//不要忘記catch //最后可以.finally(()=>({})) }
解釋
h('div')
就表示創(chuàng)建一個div標簽,- 如果寫成
h('div',{class:'...'})
就可以定義class,如:
h('i', { class: 'el-icon-question' })
- 如果寫成下面的,則可以定義props。(以element的彈出框el-tooltip為例)
h('el-tooltip',{props:{ content: (function() { return '彈出信息' })(), placement: 'top' }})
- 包含關(guān)系用
h('div',[...])
,如div中包含兩個p標簽:(可以繼續(xù)嵌套)
h('div', [ h('p', '第一個p'), h('p', '第二個p') ])
Vue的this.$confirm中注意this的指向
Vue開發(fā)過程中遇到this. confirm( )里面的this失效問題,就是當你想在里面使用data數(shù)據(jù)的時候,我們往往是 this.dataName這種方式拿到值,但在 this.confirm()里面的this失效問題,就是當你想在里面使用data數(shù)據(jù)的時候,我們往往是this.dataName這種方式拿到值,但在this. confirm()里面的this失效問題,就是當你想在里面使用data數(shù)據(jù)的時候,我們往往是this.dataName這種方式拿到值,但在this.confirm()里面的this不是指向當前vue了,所以是取不到data的數(shù)據(jù)。
解決方法
因此我們在使用this.$confirm()前先保存this
let _this = this
const _this = this this.$confirm({ title: '當前郵件正文內(nèi)容爲空', content: h => <div style="color:red;">確認是否發(fā)佈?</div>, onOk () { console.log('保存提交的對象', this.objData) _this.loading = true initAxios.saveMail(_this.objData).then((res) => { _this.loading = false if (res.data.code === '200' && res.data.result) { _this.$router.go(-1) // 處理返回需要點兩次的問題 _this.$message.success('發(fā)佈成功!') } }) }, onCancel () { return false } })
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于element ui中el-cascader的使用方式
這篇文章主要介紹了關(guān)于element ui中el-cascader的使用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09vue 監(jiān)聽input輸入事件(oninput)的示例代碼支持模糊查詢
這篇文章主要介紹了vue 監(jiān)聽input輸入事件(oninput)支持模糊查詢,比如說表格模糊查詢,實現(xiàn)一邊輸入,一邊過濾數(shù)據(jù),本文通過示例代碼給大家詳細講解,需要的朋友可以參考下2023-02-02vue?實現(xiàn)列表跳轉(zhuǎn)至詳情且能添加至購物車功能
列表頁面顯示數(shù)據(jù),點擊跳轉(zhuǎn)到對應(yīng)的詳情頁,詳情頁可以添加并跳轉(zhuǎn)到購物車,購物車具有常見功能,這篇文章主要介紹了vue?實現(xiàn)列表跳轉(zhuǎn)至詳情且能添加至購物車,需要的朋友可以參考下2022-10-10vue 循環(huán)加載數(shù)據(jù)并獲取第一條記錄的方法
今天小編就為大家分享一篇vue 循環(huán)加載數(shù)據(jù)并獲取第一條記錄的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09