Vue自定义指令

自定义指令

根据自定义的指令,可以封装一些dom操作,扩展额外的功能

  • 全局注册-语法
    全局注册是在min.js文件中去定义的
Vue.directive('指令名',{
    // inserted: 钩子是一个自定义指令的生命周期钩子函数之一。它会在被绑定的元素插入到父节点时调用。
    "inserted"(el){
        // 可以对el标签,扩展额外的功能
        el.focus();//聚焦
    }
})

image

实现效果:自动聚焦

  • 局部注册-语法
    局部注册在组件中去定义
directives:{
    '指令名':{
    // inserted: 钩子是一个自定义指令的生命周期钩子函数之一。它会在被绑定的元素插入到父节点时调用。
    inserted(el){
        // 可以对el标签,扩展额外的功能
        el.focus();//聚焦
    }
  }
}

image

  • 使用
    <input v-指令名 type ='text'/>
    只要使用的标签被绑上了自定义的指令,那么由于inserted的特殊性,就会去触发相应的操作

场景需求

需求:实现一个color指令,传入不同的颜色,给标签设置文字颜色

  • 语法:在绑定指令时,可以通过“等号”的形式为指令绑定 具体的参数值
    <h1 v-color="color">自定义指令</h1>
    其中 v-color 是我们自定义的指令, color 是参数
  • 通过binding.value可以拿到指令值,指令值修改会触发update函数
 directives: {
    color: {
      // el是markup中指令所在的元素,binding是 包含指令信息的对象
      inserted(el, binding) {
        console.log(binding.value);
        el.style.color = binding.color;
      },
      // 会在包含指令的元素的绑定值更新时触发。具体来说,当指令所绑定的值发生改变时,update 钩子会被调用。
      update(el, binding) {
        console.log(binding.value);
        el.style.color = binding.color;
      },
    },
}

完整演示:
image

image

v-loading 指令封装

场景:实际开发过程中,发送请求需要时间,在请求的数据未回来时,页面会处于空白状态,对用户的体验不好
需求:封装一个 v-loading 指令,实现加载中的效果
应用:在需要使用加载效果的盒子中使用 v-loading 就可以实现这个效果了。

分析:

  • 本质loading效果就是一个蒙层,盖在盒子上
  • 数据请求中,开启loading状态,添加蒙层
  • 数据请求完毕,关闭loading状态,移除蒙层

实现:

  • 准备一个loading类,通过伪元素定位,设置宽高,实现蒙层
  • 开启关闭loading状态,本质只需要添加移除类即可
  • 结合自定义指令的语法进行封装服用
/* 伪类 - 蒙层效果 */
.loading:before {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: #fff url('./loading.gif') no-repeat center;
}

代码实现

<template>
  <div class="main">
    <!-- isLoading:状态,当ture时,蒙层添加,false时蒙层异常,条件,当数组有数据后改为false-->
    <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>
</template>

自定义v-loading

<script>
// 安装axios =>  yarn add axios
import axios from "axios";

// 接口地址:http://hmajax.itheima.net/api/news
// 请求方式:get
export default {
  data() {
    return {
      list: [],
      isLoading: true, // 蒙层的状态
    };
  },

  async created() {
    // 1. 发送请求获取数据
    const res = await axios.get("http://hmajax.itheima.net/api/news");

    setTimeout(() => {
      // 2. 更新到 list 中
      this.list = res.data.data;
      this.isLoading = false; // 移除蒙层状态
    }, 2000);
  },
  directives: {
    loading: {
      inserted(el, binding) {
        if (binding.value) {
          el.classList.add("loading");
        } else {
          el.classList.remove("loading");
        }
      },
      update(el, binding) {
        if (binding.value) {
          el.classList.add("loading");
        } else {
          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;
}
</style>
posted @   自学Java笔记本  阅读(13)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示