vue自定義加載指令v-loading占位圖指令v-showimg
了解自定義指令的鉤子函數(shù)
bind(){}:每當(dāng)指令綁定到元素上的時(shí)候,就會(huì)立刻執(zhí)行bind這個(gè)函數(shù)。只調(diào)用一次。
和css相關(guān)的操作,可以放在這個(gè)鉤子函數(shù)中。
inserted(){}:元素插入到DOM中的時(shí)候,會(huì)執(zhí)行inserted函數(shù)。只調(diào)用一次。
update(){}當(dāng)數(shù)據(jù)跟新的時(shí)候,就會(huì)執(zhí)行updated,可能會(huì)觸發(fā)多次
可以通過(guò) bing.value(新值) !== bing.oldValue(舊值) 來(lái)判斷跟新的時(shí)候做的處理
componentUpdated(){}指令所在組件的 VNode 及其子 VNode 全部更新后調(diào)用
unbind(){}:只調(diào)用一次,指令與元素解綁時(shí)調(diào)用
所有的鉤子函數(shù)的參數(shù)都有以下:
el:指令所綁定的元素,可以用來(lái)直接操作 DOM
binding:一個(gè)對(duì)象
注冊(cè)成為全局指令
//main.js文件中
Vue.directive("color", {
鉤子函數(shù)
})
需求描述
做一個(gè)加載動(dòng)畫
在我們請(qǐng)求接口的時(shí)候,顯示加載動(dòng)畫。
當(dāng)我們請(qǐng)求成功后,移除加載動(dòng)畫。
我們將會(huì)通過(guò)自定義指令 v-loading="isLoadFlag"來(lái)控制加載動(dòng)畫的開(kāi)啟和移除。
我們將會(huì)在頁(yè)面中使用 ListCom.vue 列表組件。
加載動(dòng)畫組件 LoadingCom.vue。
自定義鉤子 loading.js
列表組件 ListCom.vue
<template>
<div class="combox">
<div v-for="(item,index) in listArr" :key="index">
人物{{ item.name }}---- 描述 {{ item.dec}}
</div>
</div>
</template>
<script>
export default {
props: {
listArr: {
type: Array,
default: () => []
},
},
}
</script>
加載動(dòng)畫組件 LoadingCom.vue
<template>
<div class="loadingcssbox">
<img src="../../assets/loading.gif"/>
</div>
</template>
鉤子函數(shù) loading.js
import Vue from 'vue'
//引入加載動(dòng)畫組件
import LoadingCom from './LoadingCom.vue'
const loadingDirectiive = {
inserted(el, bing) {
// el ==>表示被綁定了指令的那個(gè)元素,這個(gè)el是一個(gè)原生的js對(duì)象。
// bing ==> 指令相關(guān)的信息
// 得到一個(gè)組件的構(gòu)造函數(shù)
const loadingCtor = Vue.extend(LoadingCom)
// 得到實(shí)例loadingComp
const loadingComp = new loadingCtor()
// 獲取實(shí)例的html
const htmlLoading = loadingComp.$mount().$el
// 將html放在el的實(shí)例上面去
el.myHtml = htmlLoading
if (bing.value) {
appendHtml(el)
}
},
update(el, bing) {
// bing.value 是v-loading綁定的那個(gè)值; true 要顯示加載動(dòng)畫
//新值 bing.value與舊值bing.oldValue是否相等
if (bing.value !== bing.oldValue ) {
bing.value ? appendHtml(el) : removeHtml(el)
}
}
}
function appendHtml(el) {
el.appendChild(el.myHtml)
}
function removeHtml(el) {
el.removeChild(el.myHtml)
}
export default loadingDirectiive
分析上面的代碼
// 得到一個(gè)組件的構(gòu)造函數(shù) const loadingCtor = Vue.extend(LoadingCom) // 得到實(shí)例loadingComp const loadingComp = new loadingCtor() // 獲取實(shí)例的html。將html放在el的實(shí)例上面去。 // 放在el實(shí)例上的優(yōu)勢(shì)是方便訪問(wèn)。 // $el是可以訪問(wèn)加載動(dòng)畫的html。 // 這樣就可以將它添加到某一個(gè)節(jié)點(diǎn)上。同時(shí)也方便移除 const htmlLoading = loadingComp.$mount().$el el.myHtml = htmlLoading
main.js 中 注冊(cè)成為全局指令
import loadingDirectiive from './components/loading/loading'
Vue.directive('loading', loadingDirectiive)
<!-- 全局指令的注冊(cè)方式 -->
Vue.directive("color", {
鉤子函數(shù)
})
頁(yè)面中使用加載動(dòng)畫指令 v-loading
<template>
<div class="box">
<ListCom :listArr="listArr" v-loading="isLoadFlag"></ListCom>
</div>
</template>
<script>
import ListCom from '../components/ListCom.vue'
export default {
components: {
ListCom
},
data() {
return {
listArr: [],
//通過(guò)isLoadFlag來(lái)控制動(dòng)畫是否開(kāi)啟
isLoadFlag:false,
}
},
created() {
this.sendAPi()
},
methods: {
sendAPi() {
this.isLoadFlag = true;//開(kāi)啟加載動(dòng)畫
setTimeout(() => {
this.listArr = [
{ name: '齊玄幀', dec: '五百年前的呂祖', },
{ name: '王仙芝', dec: '白帝轉(zhuǎn)世' },
{ name: '李淳罡', dec: '李淳罡當(dāng)初的實(shí)力是絕對(duì)的天下第一的' },
{ name: '鄧太阿', dec: '徐鳳年的舅舅' },
{ name: '曹長(zhǎng)卿', dec: '這位號(hào)稱獨(dú)占天象八斗風(fēng)流,實(shí)力排進(jìn)天下前三' }
]
//移除加載動(dòng)畫
this.isLoadFlag = false
},3000)
}
}
}
</script>

占用圖指令 v-showimg
當(dāng)沒(méi)有數(shù)據(jù)的時(shí)候,顯示一個(gè)占位圖。
我們將會(huì)通過(guò)自定義指令 v-showimg="showImgFlag"來(lái)控制是否顯示占位圖
占位圖組件 ShowImgCom.vue。
自定義鉤子 showimg.js
廢話不多說(shuō),直接上代碼。
占位圖組件 ShowImgCom.vue
<template>
<div class="showimgbox">
<img src="../../assets/nodata.png"/>
<p>暫無(wú)數(shù)據(jù)</p>
</div>
</template>
指令的書寫 showimg.js
import Vue from 'vue'
import ShowImgCom from './ShowImgCom.vue'
const showimgDirectiive = {
inserted(el, bing) {
const showimgCtor = Vue.extend(ShowImgCom)
const showImgComp = new showimgCtor()
const htmlSHowPic = showImgComp.$mount().$el
el.myHtml = htmlSHowPic
if (bing.value) {
appendHtml(el)
}
},
update(el, bing) {
if (bing.value !== bing.oldValue) {
bing.value ? appendHtml(el) : removeHtml(el)
}
}
}
function appendHtml(el) {
el.appendChild(el.myHtml)
}
function removeHtml(el) {
el.removeChild(el.myHtml)
}
export default showimgDirectiive
main.js注冊(cè)
import showimgDirectiive from './components/ShowImg/showimg'
Vue.directive('showimg', showimgDirectiive)
指令的使用v-showimg指令
<template>
<div class="box" v-showimg="showImgFlag">
<ListCom :listArr="listArr" v-loading="isLoadFlag"></ListCom>
</div>
</template>
<script>
import ListCom from '../components/ListCom.vue'
export default {
components: {
ListCom
},
data() {
return {
listArr: [],
isLoadFlag: false,
showImgFlag:false
}
},
created() {
this.sendAPi()
},
methods: {
sendAPi() {
this.isLoadFlag = true;//加載中
setTimeout(() => {
this.listArr = []
this.isLoadFlag = false
//是否顯示占位圖
if (this.listArr && this.listArr.length === 0) {
this.showImgFlag = true
} else {
this.showImgFlag =false
}
},3000)
}
}
}
</script>

以上就是vue自定義加載指令v-loading占位圖指令v-showimg的詳細(xì)內(nèi)容,更多關(guān)于vue自定義加載占位圖指令的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
快速解決vue-cli不能初始化webpack模板的問(wèn)題
下面小編就為大家分享一篇快速解決vue-cli不能初始化webpack模板的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
關(guān)于vue-resource報(bào)錯(cuò)450的解決方案
本篇文章主要介紹關(guān)于vue-resource報(bào)錯(cuò)450的解決方案,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07
vue 使用Jade模板寫html,stylus寫css的方法
這篇文章主要介紹了vue 使用Jade模板寫html,stylus寫css的方法,文中還給大家提到了使用jade注意事項(xiàng),需要的朋友可以參考下2018-02-02
vue事件監(jiān)聽(tīng)函數(shù)on中的this指針域使用
這篇文章主要介紹了vue事件監(jiān)聽(tīng)函數(shù)on中的this指針域使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
vue 如何將二維數(shù)組轉(zhuǎn)化為一維數(shù)組
這篇文章主要介紹了vue 如何將二維數(shù)組轉(zhuǎn)化為一維數(shù)組,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
Vue.js仿Metronic高級(jí)表格(一)靜態(tài)設(shè)計(jì)
這篇文章主要為大家詳細(xì)介紹了Vue.js仿Metronic高級(jí)表格的靜態(tài)設(shè)計(jì),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
Vue3 computed初始化獲取設(shè)置值實(shí)現(xiàn)示例
這篇文章主要為大家介紹了Vue3 computed初始化以及獲取值設(shè)置值實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
解決vue無(wú)法偵聽(tīng)數(shù)組及對(duì)象屬性的變化問(wèn)題
這篇文章主要介紹了解決vue無(wú)法偵聽(tīng)數(shù)組及對(duì)象屬性的變化問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07
解決vue.js 數(shù)據(jù)渲染成功仍報(bào)錯(cuò)的問(wèn)題
今天小編就為大家分享一篇解決vue.js 數(shù)據(jù)渲染成功仍報(bào)錯(cuò)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08

