Vue (5)

自定义指令

1.基本语法(全局、局部注册)

概念:自己定义的指令,可以封装一些DOM操作,扩展额外的功能

语法:

  • 全局注册

    //在main.js中
    Vue.directive('指令名', {
      "inserted" (el) {
        // 可以对 el 标签,扩展额外功能
        el.focus()
      }
    })
  • 局部注册

    //在Vue组件的配置项中
    directives: {
      "指令名": {
        inserted (el) {
          // 可以对 el 标签,扩展额外功能
          el.focus()
        }
      }
    }

inserted:被绑定元素插入父节点时调用的钩子函数

el:使用指令的那个DOM元素

  • 使用指令

    注意:在使用指令的时候,一定要先注册再使用,否则会报错
    使用指令语法: v-指令名。如:

    注册指令时不用v-前缀,但使用时一定要加v-前缀

2.指令的值

语法

1.在绑定指令时,可以通过“等号”的形式为指令 绑定 具体的参数值

<div v-color="color">我是内容</div>

2.通过 binding.value 可以拿到指令值,指令值修改会 触发 update 函数

directives: {
  color: {
    inserted (el, binding) {
      el.style.color = binding.value
    },
    update (el, binding) {
      el.style.color = binding.value
    }
  }
}
<template>
  <div>
    <h1 v-color="color1">指令的值1测试</h1>
    <h1 v-color="color2">指令的值2测试</h1>
  </div>
</template>

<script>
export default {
  data () {
    return {
      color1: 'red',
      color2: 'pink'
    }
  },
  directives:{
    color: {
      //inserted提供的是元素被添加到页面中时的逻辑
      inserted(el, binding) {
        //binding.value就是指令的值
       el.style.color = binding.value
      },
      //update指令的值被修改的时候触发,提供值变化后dom更新的逻辑
      update (el, binding) { 
       el.style.color = binding.value
  }
    }
  }
}
</script>

<style>

</style>

3.v-loading 指令封装

1.场景

实际开发过程中,发送请求需要时间,在请求的数据未回来时,页面会处于空白状态 => 用户体验不好

2.需求

封装一个 v-loading 指令,实现加载中的效果

3.分析

1.本质 loading效果就是一个蒙层,盖在了盒子上

2.数据请求中,开启loading状态,添加蒙层

3.数据请求完毕,关闭loading状态,移除蒙层

4.实现

1.准备一个 loading类,通过伪元素定位,设置宽高,实现蒙层

2.开启关闭 loading状态(添加移除蒙层),本质只需要添加移除类即可

3.结合自定义指令的语法进行封装复用

.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">
    <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>

<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) {
      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>

插槽

1.默认插槽

1.作用:让组件内部的一些 结构 支持 自定义

2.需求:将需要多次显示的对话框,封装成一个组件

3.问题:组件的内容部分,不希望写死,希望能使用的时候自定义。怎么办

4.插槽的基本语法

  1. 组件内需要定制的结构部分,改用<slot></slot>占位
  2. 使用组件时, <MyDialog></MyDialog>标签内部, 传入结构替换slot
  3. 给插槽传入内容时,可以传入纯文本、html标签、组件

 2.后备内容(默认值)

插槽的后备内容:封装组件时,可以为预留的 <slot> 插槽提供后备内容(默认内容)

语法:在 标签内,放置内容, 作为默认显示内容

效果:

  • 外部使用组件时,不传东西,则slot会显示后备内容

    <MyDialog></MyDialog>

  • 外部使用组件时,传东西了,则slot整体会被换掉

    <MyDialog>我是内容</MyDialog>

3.具名插槽

语法:

1. 多个slot使用name属性区分名字

div class="dialog-header">
       <slot name="head"></slot>
     </div>
     <div class="dialog-content">
       <slot name="content"></slot>
     </div>
     <div class="dialog-footer">
       <slot name="footer"></slot>
</div>

2. template配合v-slot:名字来分发对应标签

<MyDialog>
   <template v-slot:head>
      大标题
   </template>
   <template v-slot:content>
      内容文本
   </template>
   <template v-slot:footer>
   <button>按钮</button>
   </template>
</MyDialog>

3. v-slot:插槽名 可以简化成 #插槽名

4.作用域插槽

1.插槽分类

  • 默认插槽(组件内定制一处结构)

  • 具名插槽(组件内定制多处结构)

    插槽只有两种,作用域插槽不属于插槽的一种分类

2.作用

定义slot 插槽的同时, 是可以传值的。给 插槽 上可以 绑定数据,将来 使用组件时可以用

3.使用步骤

  1. 给 slot 标签, 以 添加属性的方式传值

    <slot :id="item.id" msg="测试文本"></slot>
    
  2. 所有添加的属性, 都会被收集到一个对象中

    { id: 3, msg: '测试文本' }
    
  3. 在template中, 通过 #插槽名= "obj" 接收,默认插槽名为 default

    <MyTable :list="list">
      <template #default="obj">
        <button @click="del(obj.id)">删除</button>
      </template>
    </MyTable>

路由入门

1.单页应用程序

1.概念

单页应用程序:SPA【Single Page Application】是指所有的功能都在一个html页面上实现

2.具体示例

单页应用网站: 网易云音乐 网易云音乐

多页应用网站:京东 京东(JD.COM)-正品低价、品质保障、配送及时、轻松购物!

3.单页应用 VS 多页面应用

单页应用类网站:系统类网站 / 内部网站 / 文档类网站 / 移动端站点

多页应用类网站:公司官网 / 电商类网站

 2.路由概念

Vue中的路由:路径和组件映射关系

3.路由的基本使用

1.作用:插件 VueRouter修改地址栏路径时,切换显示匹配的组件

2.VueRouter的使用(5+2)

  1. 下载 VueRouter 模块到当前工程,版本3.6.5

    yarn add vue-router@3.6.5
  2. main.js中引入VueRouter

    import VueRouter from 'vue-router'
    
  3. 安装注册

    Vue.use(VueRouter)
    
  4. 创建路由对象

    const router = new VueRouter()
    
  5. 注入,将路由对象注入到new Vue实例中,建立关联

    new Vue({
      render: h => h(App),
      router:router
    }).$mount('#app')
    
    

当配置完以上5步后 就可以看到浏览器地址栏中的路由 变成了 /#/的形式。表示项目的路由已经被Vue-Router管理了

两个核心步骤:

1.创建需要的组件 (views目录),配置路由规则

2.配置导航,配置路由出口(路径匹配的组件显示的位置)

App.vue

<div class="footer_wrap">
  <a href="#/find">发现音乐</a>
  <a href="#/my">我的音乐</a>
  <a href="#/friend">朋友</a>
</div>
<div class="top">
  <router-view></router-view>
</div>

4.组件的存放目录问题

1.组件分类

.vue文件分为2类,都是 .vue文件(本质无区别)

  • 页面组件 (配置路由规则时使用的组件)
  • 复用组件(多个组件中都使用到的组件)

2.存放目录

分类开来的目的就是为了 更易维护

  1. src/views文件夹

    页面组件 - 页面展示 - 配合路由用

  2. src/components文件夹

    复用组件 - 展示数据 - 常用于复用

5.路由的封装抽离

目标:将路由模块抽离出来。 好处:拆分模块,利于维护

路径简写:

脚手架环境下 @指代src目录,可以用于快速引入组件

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值