首页 > 其他分享 >学习vue3(六)(深入v-model,自定义指令directive,组合式函数(自定义Hooks))

学习vue3(六)(深入v-model,自定义指令directive,组合式函数(自定义Hooks))

时间:2022-11-03 15:01:44浏览次数:83  
标签:el vue const 自定义 directive Hooks 指令 import

深入v-model

TIps 在Vue3 v-model 是破坏性更新的

v-model在组件里面也是很重要的

v-model 其实是一个语法糖 通过props 和 emit组合而成的

1.默认值的改变

prop:value -> modelValue;
事件:input -> update:modelValue;
v-bind 的 .sync 修饰符和组件的 model 选项已移除
新增 支持多个v-model
新增 支持自定义 修饰符 Modifiers
案例 子组件

<template>
     <div v-if='propData.modelValue ' class="dialog">
         <div class="dialog-header">
             <div>标题</div><div @click="close">x</div>
         </div>
         <div class="dialog-content">
            内容
         </div>
         
     </div>
</template>
 
<script setup lang='ts'>
 
type Props = {
   modelValue:boolean
}
 
const propData = defineProps<Props>()
 
const emit = defineEmits(['update:modelValue'])
 
const close = () => {
     emit('update:modelValue',false)
}
 
</script>
 
<style lang='less'>
.dialog{
    width: 300px;
    height: 300px;
    border: 1px solid #ccc;
    position: fixed;
    left:50%;
    top:50%;
    transform: translate(-50%,-50%);
    &-header{
        border-bottom: 1px solid #ccc;
        display: flex;
        justify-content: space-between;
        padding: 10px;
    }
    &-content{
        padding: 10px;
    }
}
</style>

  

父组件

<template>
  <button @click="show = !show">开关{{show}}</button>
  <HelloWorld v-model="show"></HelloWorld>
</template>
 
<script setup lang='ts'>
import HelloWorld from "../components/HelloWorld.vue";
import {ref} from 'vue'
const show = ref(false)
</script>
 
<style>
</style>

  

绑定多个案例

 子组件

<template>
     <div v-if='modelValue ' class="dialog">
         <div class="dialog-header">
             <div>标题---{{title}}</div><div @click="close">x</div>
         </div>
         <div class="dialog-content">
            内容
         </div>
         
     </div>
</template>
 
<script setup lang='ts'>
 
type Props = {
   modelValue:boolean,
   title:string
}
 
const propData = defineProps<Props>()
 
const emit = defineEmits(['update:modelValue','update:title'])
 
const close = () => {
     emit('update:modelValue',false)
     emit('update:title','我要改变')
}
 
</script>
 
<style lang='less'>
.dialog{
    width: 300px;
    height: 300px;
    border: 1px solid #ccc;
    position: fixed;
    left:50%;
    top:50%;
    transform: translate(-50%,-50%);
    &-header{
        border-bottom: 1px solid #ccc;
        display: flex;
        justify-content: space-between;
        padding: 10px;
    }
    &-content{
        padding: 10px;
    }
}
</style>

 

父组件

<template>
  <button @click="show = !show">开关{{show}} ----- {{title}}</button>
  <HelloWorld v-model:title='title' v-model="show"></HelloWorld>
</template>
 
<script setup lang='ts'>
import HelloWorld from "../components/HelloWorld.vue";
import {ref} from 'vue'
const show = ref(false)
const title = ref('我是标题')
</script>
 
<style>
</style>

  

自定义指令directive

directive-自定义指令(属于破坏性更新)
Vue中有v-if,v-for,v-bind,v-show,v-model 等等一系列方便快捷的指令 今天一起来了解一下vue里提供的自定义指令

1.Vue3指令的钩子函数
created 元素初始化的时候
beforeMount 指令绑定到元素后调用 只调用一次
mounted 元素插入父级dom调用
beforeUpdate 元素被更新之前调用
update 这个周期方法被移除 改用updated
beforeUnmount 在元素被移除前调用
unmounted 指令被移除后调用 只调用一次
Vue2 指令 bind inserted update componentUpdated unbind

2.在setup内定义局部指令
但这里有一个需要注意的限制:必须以 vNameOfDirective 的形式来命名本地自定义指令,以使得它们可以直接在模板中使用。

<template>

  <HelloWorld  v-move-directive="{background:'green'}"></HelloWorld>

</template>
 
<script setup lang='ts'>
import HelloWorld from "../components/HelloWorld.vue";
import {ref,Directive,DirectiveBinding} from 'vue'
 
const vMoveDirective: Directive = {
  created: () => {
    console.log("初始化====>");
  },
  beforeMount(...args: Array<any>) {
    // 在元素上做些操作
    console.log("初始化一次=======>");
  },
  mounted(el: any, dir: DirectiveBinding<any>) {
    el.style.background = dir.value.background;
    console.log("初始化========>");
  },
  beforeUpdate() {
    console.log("更新之前");
  },
  updated() {
    console.log("更新结束");
  },
  beforeUnmount(...args: Array<any>) {
    console.log(args);
    console.log("======>卸载之前");
  },
  unmounted(...args: Array<any>) {
    console.log(args);
    console.log("======>卸载完成");
  },
};
</script>
 
<style>
</style>

  

3.生命周期钩子参数详解
第一个 el 当前绑定的DOM 元素

第二个 binding

instance:使用指令的组件实例。
value:传递给指令的值。例如,在 v-my-directive="1 + 1" 中,该值为 2。
oldValue:先前的值,仅在 beforeUpdate 和 updated 中可用。无论值是否有更改都可用。
arg:传递给指令的参数(如果有的话)。例如在 v-my-directive:foo 中,arg 为 "foo"。
modifiers:包含修饰符(如果有的话) 的对象。例如在 v-my-directive.foo.bar 中,修饰符对象为 {foo: true,bar: true}。
dir:一个对象,在注册指令时作为参数传递。例如,在以下指令中

 

 


第三个 当前元素的虚拟DOM 也就是Vnode

第四个 prevNode 上一个虚拟节点,仅在 beforeUpdate 和 updated 钩子中可用

4.函数简写
你可能想在 mounted 和 updated 时触发相同行为,而不关心其他的钩子函数。那么你可以通过将这个函数模式实现

<template>
   <div>
      <input v-model="value" type="text" />
      <HelloWorld v-move="{ background: value }"></HelloWorld>
   </div>
</template>
   
<script setup lang='ts'>
import HelloWorld from '../components/HelloWorld.vue'
import { ref, Directive, DirectiveBinding } from 'vue'
let value = ref<string>('')
type Dir = {
   background: string
}
const vMove: Directive = (el, binding: DirectiveBinding<Dir>) => {
   el.style.background = binding.value.background
}
</script>
 
 
 
<style>
</style>

案例自定义拖拽指令   

<template>
  <div v-move class="box">
    <div class="header"></div>
    <div>
      内容
    </div>
  </div>
</template>
 
<script setup lang='ts'>
import { Directive } from "vue";
const vMove: Directive = {
  mounted(el: HTMLElement) {
    let moveEl = el.firstElementChild as HTMLElement;
    const mouseDown = (e: MouseEvent) => {
      //鼠标点击物体那一刻相对于物体左侧边框的距离=点击时的位置相对于浏览器最左边的距离-物体左边框相对于浏览器最左边的距离
      console.log(e.clientX, e.clientY, "-----起始", el.offsetLeft);
      let X = e.clientX - el.offsetLeft;
      let Y = e.clientY - el.offsetTop;
      const move = (e: MouseEvent) => {
        el.style.left = e.clientX - X + "px";
        el.style.top = e.clientY - Y + "px";
        console.log(e.clientX, e.clientY, "---改变");
      };
      document.addEventListener("mousemove", move);
      document.addEventListener("mouseup", () => {
        document.removeEventListener("mousemove", move);
      });
    };
    moveEl.addEventListener("mousedown", mouseDown);
  },
};
</script>
 
<style lang='less'>
.box {
  position: fixed;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: 200px;
  height: 200px;
  border: 1px solid #ccc;
  .header {
    height: 20px;
    background: black;
    cursor: move;
  }
}
</style>

  

组合式函数(自定义Hooks)

主要用来处理复用代码逻辑的一些封装

这个在vue2 就已经有一个东西是Mixins

mixins就是将这些多个相同的逻辑抽离出来,各个组件只需要引入mixins,就能实现一次写代码,多组件受益的效果。

弊端就是 会涉及到覆盖的问题,变量来源不明确(隐式传入),不利于阅读,使代码变得难以维护

Vue3 的自定义的hook

Vue3 的 hook函数 相当于 vue2 的 mixin, 不同在与 hooks 是函数
Vue3 的 hook函数 可以帮助我们提高代码的复用性, 让我们能在不同的组件中都利用 hooks 函数
Vue3 hook 库Get Started | VueUse

案例
新建ts结尾文件

import { onMounted } from 'vue'
 
 
type Options = {
    el: string
}
 
type Return = {
    Baseurl: string | null
}
export default function (option: Options): Promise<Return> {
    
    return new Promise((resolve) => {
        onMounted(() => {
            const file: HTMLImageElement = document.querySelector(option.el) as HTMLImageElement;
            file.onload = ():void => {
                resolve({
                    Baseurl: toBase64(file)
                })
            }
 
        })
 
 
        const toBase64 = (el: HTMLImageElement): string => {
            const canvas: HTMLCanvasElement = document.createElement('canvas')
            const ctx = canvas.getContext('2d') as CanvasRenderingContext2D
            canvas.width = el.width
            canvas.height = el.height
            ctx.drawImage(el, 0, 0, canvas.width,canvas.height)
            
            return canvas.toDataURL('image/png')
 
        }
    })
 
 
}

  

引用自定义hook

<template>
  <div>
    <img id="img" width="200" height="200" src="../assets/logo.png" alt="">
  </div>
</template>
 
<script setup lang='ts'>
import useCanvas from '../hook/useCanvas'
import { ref } from "vue";
useCanvas({el:'#img'}).then((res)=>{
  console.log(res)
})
</script>
 
<style lang='less'>

</style>

 


————————————————
版权声明:本文为CSDN博主「小满zs」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq1195566313/article/details/123187523

标签:el,vue,const,自定义,directive,Hooks,指令,import
From: https://www.cnblogs.com/xiaobaibubai/p/16854461.html

相关文章

  • wpf VS2017 带图片显示的自定义Combox
    先看下效果图  思路大概是将ComboxItem分为4列,然后将下拉框选中的值设置到Combox中首先新建一个wpf的工程,取名为PictureCombox1.添加需要用的png图,先导入图片两张,取......
  • 全局Loading(自定义Loading)
    在调用的方法中加入(中间注释部分,为请求数据方法,结束都需要关闭loading)constloading=this.$loading({lock:true,text:'正在努力加载数据中,请耐......
  • 手把手教你用DevExpress WinForm添加和自定义工具栏皮肤选择器
    DevExpressWinForm拥有180+组件和UI库,能为WindowsForms平台创建具有影响力的业务解决方案。DevExpressWinForms能完美构建流畅、美观且易于使用的应用程序,无论是Office......
  • django中间件以及自定义中间件
    middleware中间件就是在目标和结果之间进行的额外处理过程,在Django中就是request和response之间进行的处理,相对来说实现起来比较简单,但是要注意它是对全局有效的,可以在全......
  • Struts2自定义方法最佳实践
    自定义方法实现在struts.xml配置method,并且在对应的Action实现对应方法即可。struts.xml<actionname="login2"class="space.terwer.struts23.LoginAction2"met......
  • 博客园自定义主题中添加迷你音乐插件
    说明:这里直接介绍最简单直接的一种设置方式,想深入了解,自己DIY的,可滑到本文底部,附有其他大佬的方案。首先,进入你的博客园后台设置,在开通了JS权限(可自定义博客园主......
  • vim指令diffopt:Vimdiff比较文本时自定义差异上下文要显示的行数
    Vim相关指令:在Vim窗口执行setdiffopt=filler,context:10或setdiffopt=filler,context:0命令即可(其中10为山下文的行数,设置为0即仅显示差异文本,不显示任何相当的行......
  • 自定义的组件无法显示;
      定义了一个自定义组件title,但是无论如何都显示不出来,看了title组件的created()函数的打印,也灭有打印; 调试的话,看到title组件也没有像其他组件一样通过re......
  • SpringBoot自定义注解+异步+观察者模式实现业务日志保存
    一、前言我们在企业级的开发中,必不可少的是对日志的记录,实现有很多种方式,常见的就是基于AOP+注解进行保存,但是考虑到程序的流畅和效率,我们可以使用异步进行保存,在高并发情......
  • 树莓派搭建WordPress博客:解析自定义域名 9/10
    上一篇​​树莓派搭建WordPress博客:更换WordPress主体模板8/10​​在之前的系列文章中,我们向大家介绍了如何在本地树莓派上搭建属于自己的网站,并让这个网站能被公众互联网......