最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue自定義指令簡(jiǎn)介和基本使用示例

 更新時(shí)間:2024年03月30日 10:07:04   作者:程序員無(wú)羨  
同時(shí)Vue也支持讓開(kāi)發(fā)者,自己注冊(cè)一些指令,這些指令被稱為自定義指令,每個(gè)指令都有自己各自獨(dú)立的功能,這篇文章主要介紹了Vue自定義指令簡(jiǎn)介和基本使用,需要的朋友可以參考

自定義指令

1.指令介紹

  • 內(nèi)置指令:v-html、v-if、v-bind、v-on… 這都是Vue給咱們內(nèi)置的一些指令,可以直接使用
  • 自定義指令:同時(shí)Vue也支持讓開(kāi)發(fā)者,自己注冊(cè)一些指令。這些指令被稱為自定義指令

每個(gè)指令都有自己各自獨(dú)立的功能

2.自定義指令

概念:自己定義的指令,可以封裝一些DOM操作,擴(kuò)展額外的功能

3.自定義指令語(yǔ)法

全局注冊(cè)

//在main.js中
Vue.directive('指令名', {
  "inserted" (el) {
    // 可以對(duì) el 標(biāo)簽,擴(kuò)展額外功能
    el.focus()
  }
})

局部注冊(cè)

//在Vue組件的配置項(xiàng)中
directives: {
  "指令名": {
    inserted () {
      // 可以對(duì) el 標(biāo)簽,擴(kuò)展額外功能
      el.focus()
    }
  }
}

使用指令

注意:在使用指令的時(shí)候,一定要先注冊(cè),再使用,否則會(huì)報(bào)錯(cuò)
使用指令語(yǔ)法: v-指令名。如:

注冊(cè)指令時(shí)不用v-前綴,但使用時(shí)一定要加v-前綴

4.指令中的配置項(xiàng)介紹

inserted:被綁定元素插入父節(jié)點(diǎn)時(shí)調(diào)用的鉤子函數(shù)

el:使用指令的那個(gè)DOM元素

5.代碼示例

需求:當(dāng)頁(yè)面加載時(shí),讓元素獲取焦點(diǎn)(autofocus在safari瀏覽器有兼容性

App.vue

<template>
  <div>
    <h1>自定義指令</h1>
    <input v-focus ref="inp" type="text">
  </div>
</template>
<script>
export default {
  // mounted () {
  //   this.$refs.inp.focus()
  // }
  // 2. 局部注冊(cè)指令
  directives: {
    // 指令名:指令的配置項(xiàng)
    focus: {
      inserted (el) {
        el.focus()
      }
    }
  }
}
</script>
<style>
</style>

main.js

import Vue from 'vue'
import App from './App.vue'
Vue.config.productionTip = false
// // 1. 全局注冊(cè)指令
// Vue.directive('focus', {
//   // inserted 會(huì)在 指令所在的元素,被插入到頁(yè)面中時(shí)觸發(fā)
//   inserted (el) {
//     // el 就是指令所綁定的元素
//     // console.log(el);
//     el.focus()
//   }
// })
new Vue({
  render: h => h(App),
}).$mount('#app')

自定義指令-指令的值

1.需求

實(shí)現(xiàn)一個(gè) color 指令 - 傳入不同的顏色, 給標(biāo)簽設(shè)置文字顏色

2.語(yǔ)法

1.在綁定指令時(shí),可以通過(guò)“等號(hào)”的形式為指令 綁定 具體的參數(shù)值

<div v-color="color">我是內(nèi)容</div>

2.通過(guò) binding.value 可以拿到指令值,指令值修改會(huì) 觸發(fā) update 函數(shù)

directives: {
  color: {
    inserted (el, binding) {
      el.style.color = binding.value
    },
    update (el, binding) {
      el.style.color = binding.value
    }
  }
}

3.代碼示例

App.vue

<template>
  <div>
    <h1 v-color="color1">指令的值1測(cè)試</h1>
    <h1 v-color="color2">指令的值2測(cè)試</h1>
  </div>
</template>
<script>
export default {
  data () {
    return {
      color1: 'red',
      color2: 'orange'
    }
  },
  directives: {
    color: {
      // 1. inserted 提供的是元素被添加到頁(yè)面中時(shí)的邏輯
      inserted (el, binding) {
        // console.log(el, binding.value);
        // binding.value 就是指令的值
        el.style.color = binding.value
      },
      // 2. update 指令的值修改的時(shí)候觸發(fā),提供值變化后,dom更新的邏輯
      update (el, binding) {
        console.log('指令的值修改了');
        el.style.color = binding.value
      }
    }
  }
}
</script>
<style>
</style>

main.js

import Vue from 'vue'
import App from './App.vue'
Vue.config.productionTip = false
new Vue({
  render: h => h(App),
}).$mount('#app')

自定義指令-v-loading指令的封裝

1.場(chǎng)景

實(shí)際開(kāi)發(fā)過(guò)程中,發(fā)送請(qǐng)求需要時(shí)間,在請(qǐng)求的數(shù)據(jù)未回來(lái)時(shí),頁(yè)面會(huì)處于空白狀態(tài) => 用戶體驗(yàn)不好

2.需求

封裝一個(gè) v-loading 指令,實(shí)現(xiàn)加載中的效果

3.分析

1.本質(zhì) loading效果就是一個(gè)蒙層,蓋在了盒子上

2.數(shù)據(jù)請(qǐng)求中,開(kāi)啟loading狀態(tài),添加蒙層

3.數(shù)據(jù)請(qǐng)求完畢,關(guān)閉loading狀態(tài),移除蒙層

4.實(shí)現(xiàn)

1.準(zhǔn)備一個(gè) loading類,通過(guò)偽元素定位,設(shè)置寬高,實(shí)現(xiàn)蒙層

2.開(kāi)啟關(guān)閉 loading狀態(tài)(添加移除蒙層),本質(zhì)只需要添加移除類即可

3.結(jié)合自定義指令的語(yǔ)法進(jìn)行封裝復(fù)用

.loading:before {
  content: "";
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: #fff url("./loading.gif") no-repeat center;
}

5.準(zhǔn)備代碼

<template>
  <div class="main">
    <div class="box" v-loading="isLoading">
      <ul>
        <li v-for="item in list" :key="item.id" class="news">
          <div class="left">
            <div class="title">{{ item.title }}</div>
            <div class="info">
              <span>{{ item.source }}</span>
              <span>{{ item.time }}</span>
            </div>
          </div>
          <div class="right">
            <img :src="item.img" alt="">
          </div>
        </li>
      </ul>
    </div>
    <div class="box2" v-loading="isLoading2"></div>
  </div>
</template>
<script>
// 安裝axios =>  yarn add axios
import axios from 'axios'
// 接口地址:http://hmajax.itheima.net/api/news
// 請(qǐng)求方式:get
export default {
  data () {
    return {
      list: [],
      isLoading: true,
      isLoading2: true
    }
  },
  async created () {
    // 1. 發(fā)送請(qǐng)求獲取數(shù)據(jù)
    const res = await axios.get('http://hmajax.itheima.net/api/news')
    setTimeout(() => {
      // 2. 更新到 list 中,用于頁(yè)面渲染 v-for
      this.list = res.data.data
      this.isLoading = false
    }, 2000)
  },
  directives: {
    loading: {
      inserted (el, binding) {
        binding.value ? el.classList.add('loading') : el.classList.remove('loading')
      },
      update (el, binding) {
        binding.value ? el.classList.add('loading') : el.classList.remove('loading')
      }
    }
  }
}
</script>
<style>
.loading:before {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: #fff url('./loading.gif') no-repeat center;
}
.box2 {
  width: 400px;
  height: 400px;
  border: 2px solid #000;
  position: relative;
}
.box {
  width: 800px;
  min-height: 500px;
  border: 3px solid orange;
  border-radius: 5px;
  position: relative;
}
.news {
  display: flex;
  height: 120px;
  width: 600px;
  margin: 0 auto;
  padding: 20px 0;
  cursor: pointer;
}
.news .left {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding-right: 10px;
}
.news .left .title {
  font-size: 20px;
}
.news .left .info {
  color: #999999;
}
.news .left .info span {
  margin-right: 20px;
}
.news .right {
  width: 160px;
  height: 120px;
}
.news .right img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
</style>

到此這篇關(guān)于Vue自定義指令簡(jiǎn)介和基本使用的文章就介紹到這了,更多相關(guān)Vue自定義指令內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue3使用SSE實(shí)現(xiàn)前端全局事件通訊方式

    vue3使用SSE實(shí)現(xiàn)前端全局事件通訊方式

    文章介紹了使用Vue3和VueUse實(shí)現(xiàn)基于SSE(Server-Sent Events)的全局事件通訊系統(tǒng),SSE具有單向通信、基于HTTP、自動(dòng)重連和輕量級(jí)等特點(diǎn),適合實(shí)時(shí)性要求高的應(yīng)用,實(shí)現(xiàn)方案包括訂閱消息、取消訂閱和關(guān)閉連接等功能
    2025-10-10
  • Vue加載讀取本地txt/json等文件的實(shí)現(xiàn)方式

    Vue加載讀取本地txt/json等文件的實(shí)現(xiàn)方式

    這篇文章主要介紹了Vue加載讀取本地txt/json等文件的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • antd Select下拉菜單動(dòng)態(tài)添加option里的內(nèi)容操作

    antd Select下拉菜單動(dòng)態(tài)添加option里的內(nèi)容操作

    這篇文章主要介紹了antd Select下拉菜單動(dòng)態(tài)添加option里的內(nèi)容操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-11-11
  • vue?element表格某一列內(nèi)容過(guò)多,超出省略號(hào)顯示的實(shí)現(xiàn)

    vue?element表格某一列內(nèi)容過(guò)多,超出省略號(hào)顯示的實(shí)現(xiàn)

    這篇文章主要介紹了vue?element表格某一列內(nèi)容過(guò)多,超出省略號(hào)顯示的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • vue監(jiān)聽(tīng)sessionStorage中值的變化方式

    vue監(jiān)聽(tīng)sessionStorage中值的變化方式

    這篇文章主要介紹了vue監(jiān)聽(tīng)sessionStorage中值的變化方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • vue項(xiàng)目base64轉(zhuǎn)img方式

    vue項(xiàng)目base64轉(zhuǎn)img方式

    這篇文章主要介紹了vue項(xiàng)目base64轉(zhuǎn)img方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • 在Echarts圖中給坐標(biāo)軸加一個(gè)標(biāo)識(shí)線markLine

    在Echarts圖中給坐標(biāo)軸加一個(gè)標(biāo)識(shí)線markLine

    這篇文章主要介紹了在Echarts圖中給坐標(biāo)軸加一個(gè)標(biāo)識(shí)線markLine,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-07-07
  • Vue分頁(yè)查詢?cè)趺磳?shí)現(xiàn)

    Vue分頁(yè)查詢?cè)趺磳?shí)現(xiàn)

    這篇文章主要介紹了Vue分頁(yè)查詢?cè)趺磳?shí)現(xiàn),使用vue實(shí)現(xiàn)分頁(yè)的邏輯并不復(fù)雜,接收后端傳輸過(guò)來(lái)的數(shù)據(jù),然后根據(jù)數(shù)據(jù)的總數(shù)和每一頁(yè)的數(shù)據(jù)量就可以計(jì)算出一共可以分成幾頁(yè)
    2023-04-04
  • 詳解Vue-cli webpack移動(dòng)端自動(dòng)化構(gòu)建rem問(wèn)題

    詳解Vue-cli webpack移動(dòng)端自動(dòng)化構(gòu)建rem問(wèn)題

    這篇文章主要介紹了詳解Vue-cli webpack移動(dòng)端自動(dòng)化構(gòu)建rem問(wèn)題,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-04-04
  • 淺談Vue CLI 3結(jié)合Lerna進(jìn)行UI框架設(shè)計(jì)

    淺談Vue CLI 3結(jié)合Lerna進(jìn)行UI框架設(shè)計(jì)

    這篇文章主要介紹了淺談Vue CLI 3結(jié)合Lerna進(jìn)行UI框架設(shè)計(jì),在此之前先簡(jiǎn)單介紹一下Element的構(gòu)建流程,以便對(duì)比新的UI框架設(shè)計(jì)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2019-04-04

最新評(píng)論

长治市| 澳门| 东阿县| 怀来县| 丰都县| 南郑县| 贞丰县| 惠东县| 积石山| 察雅县| 平顶山市| 湖南省| 武陟县| 珲春市| 察哈| 阳春市| 岢岚县| 五大连池市| 五大连池市| 甘肃省| 大城县| 宝应县| 当涂县| 金华市| 巩义市| 德兴市| 改则县| 满洲里市| 于都县| 靖州| 抚州市| 巴彦县| 南乐县| 泉州市| 凤城市| 栾川县| 托克托县| 涿鹿县| 呈贡县| 积石山| 旺苍县|