ElementUI?$notify通知方法中渲染自定義組件實(shí)現(xiàn)
一、背景
ElementUI 的 Notification
組件通常用于全局的通知提醒消息,其中展示內(nèi)容默認(rèn)是文本字符串,當(dāng)然也可以設(shè)置 dangerouslyUseHTMLString: true
后傳入 HTML 片段。
如果要展示比較復(fù)雜的動(dòng)態(tài)內(nèi)容,一般會(huì)把傳入的內(nèi)容封裝成組件,而直接傳入組件是無法渲染的,本文就是解決 $notify 中怎么渲染自定義組件的問題。
Vue && Notification
最近開發(fā)項(xiàng)目遇到一個(gè)數(shù)據(jù)同步延遲的問題,就是在提交表單后,創(chuàng)建或編輯的操作不能馬上同步更新。最后討論的解決辦法就是在提交表單之后,前端輪詢一個(gè)獲取狀態(tài)的接口,并在全局展示一個(gè)進(jìn)度條,實(shí)時(shí)更新進(jìn)度,所以就使用了 Notification 組件。
二、問題解析
this.$notify
方法中有一個(gè) message 參數(shù),類型為 string/Vue.VNode
。要想渲染一個(gè)自定義組件,關(guān)鍵就是要把自定義組件轉(zhuǎn)化為 Vue.VNode
。
Vue全局提供了一個(gè) this.$createElement
方法就是專門干這個(gè)的,用法和 render 函數(shù)中參數(shù) createElement
一致 (createElement: () => VNode) => VNode
。
三、具體實(shí)現(xiàn)
- 根組件 App.vue
<template> <div>content</div> </template> <script> import ProgressBar from '@/components/ProgressBar' export default { // 注冊(cè)自定義組件 components: { ProgressBar, }, data() { return { progress: 1, hiveData: {}, } }, methods: { showProgress () { const h = this.$createElement this.notifyInstance = this.$notify({ title: '數(shù)據(jù)處理進(jìn)度', duration: 0, dangerouslyUseHTMLString: true, message: h('ProgressBar', { // 使用自定義組件 ref: 'progressBar', props: { progress: this.progress, ...this.hiveData, }, }), }) }, setProgressVal() { this.$refs.progressBar && this.$refs.progressBar.setProgress(this.progress) }, } } </script>
- 自定義組件 ProgressBar.vue
<template> <div class="global-bar"> <div class="global-bar-label">庫(kù)名:【{{ dbName }}】</div> <div class="global-bar-label">表名:【{{ tableName }}】</div> <el-progress :text-inside="true" :stroke-width="16" :percentage="progress" :color="colors" ></el-progress> <br /> <el-alert title="關(guān)閉或刷新后不再顯示提交進(jìn)展,請(qǐng)勿關(guān)閉或刷新。" type="warning" :closable="false" show-icon > </el-alert> </div> </template> <script> export default { props: { dbName: { type: String, default: '', }, tableName: { type: String, default: '', }, }, data() { return { progress: 1, colors: [ { color: '#f56c6c', percentage: 20 }, { color: '#e6a23c', percentage: 40 }, { color: '#6f7ad3', percentage: 60 }, { color: '#1989fa', percentage: 80 }, { color: '#5cb87a', percentage: 100 }, ], hiveData: {}, } }, methods: { setProgress(progress) { this.progress = progress }, }, } </script>
- 注意:
h()
方法的第一個(gè)參數(shù)要么是原生標(biāo)簽名,如:div、p、span、h1等,要么就是components
中注冊(cè)過的自定義組件名,否則無法正常渲染。
以上就是ElementUI $notify通知方法中渲染自定義組件實(shí)現(xiàn)的詳細(xì)內(nèi)容,更多關(guān)于ElementUI $notify自定義組件的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Vue.js組件props數(shù)據(jù)驗(yàn)證實(shí)現(xiàn)詳解
這篇文章主要為大家詳細(xì)介紹了Vue.js組件props數(shù)據(jù)驗(yàn)證的實(shí)現(xiàn)方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10vue19 組建 Vue.extend component、組件模版、動(dòng)態(tài)組件 的實(shí)例代碼
這篇文章主要介紹了vue19 組建 Vue.extend component、組件模版、動(dòng)態(tài)組件 的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04element-plus 在vue3 中不生效的原因解決方法(element-plus引入)
這篇文章主要介紹了element-plus 在vue3 中不生效的原因解決方法(element-plus引入),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08vue和iview實(shí)現(xiàn)Scroll 數(shù)據(jù)無限滾動(dòng)功能
今天小編就為大家分享一篇vue和iview實(shí)現(xiàn)Scroll 數(shù)據(jù)無限滾動(dòng)功能,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-10-10Vue源碼中要const _toStr = Object.prototype.toString的原因分析
這篇文章主要介紹了Vue源碼中要const _toStr = Object.prototype.toString的原因分析,在文中給大家提到了Object.prototype.toString方法的原理,需要的朋友可以參考下2018-12-12Vue3封裝ElImageViewer預(yù)覽圖片的示例代碼
本文主要介紹了Vue3封裝ElImageViewer預(yù)覽圖片的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07Vue3使用ECharts實(shí)現(xiàn)桑基圖的代碼示例
?;鶊D是一種用于直觀顯示流向數(shù)據(jù)的可視化工具,特別適合展示復(fù)雜的網(wǎng)絡(luò)關(guān)系和資源流動(dòng),在前端項(xiàng)目中,通過結(jié)合?Vue?3?和?ECharts,可以快速實(shí)現(xiàn)交互性強(qiáng)、樣式美觀的?;鶊D,本文將通過完整的代碼示例,帶你一步步完成一個(gè)桑基圖的實(shí)現(xiàn),需要的朋友可以參考下2025-01-01