• VUE3学习 第三章 认识 ref全家桶、Reactive全家桶、to系列全家桶、computed计算属性、watch侦听器、watchEffect高级侦听器


    一、ref全家桶

    1. ref

    接受一个内部值并返回一个响应式且可变的 ref 对象。ref 对象仅有一个 .value property,指向该内部值。

    案例:
    我们这样操作是无法改变message 的值 应为message 不是响应式的无法被vue 跟踪要改成ref

    <template>
      <div>
        <button @click="changeMsg">change</button>
        <div>{
       {
        message }}</div>
      </div>
    </template>
     
    <script setup lang="ts">
    let message: string = "我是message"
     
    const changeMsg = () => {
       
       message = "change msg"
    }
    </script>
     
    <style>
    </style>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    需要改为 ref 、 Ref TS对应的接口

    
    interface Ref<T> {
       
      value: T
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    注意被ref包装之后需要.value 来进行赋值

    2. isRef

    用来 判断是不是一个ref对象

    import {
       ref, Ref, isRef} from 'vue'
    // 这里 ref 是做双向绑定用的   Ref 是TS的用法
    let message: Ref<string | number> = ref("111")
    let notRef: number = 222
    const changeMsg = () => {
       
      console.log(isRef(message)); //true
      console.log(isRef(notRef)); //false
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    3. ref浏览器显示配置

    ref在浏览器的console输出显示过于复杂,可以配置一下浏览器设置
    在F12中 点击设置 勾选 启动自定义格式设置工具即可
    次数 显示的 log 为 Ref<“111”>

    4. shallowRef

    创建一个跟踪自身 .value 变化的 ref,但不会使其值也变成响应式的 ( 注意! 一定不要和 ref 一起写, 不然他的值也会变成响应式)

    例子:
    修改其属性是非响应式的这样是不会改变的

    
    <template>
      <div>
        <button @click="changeMsg">change</button>
        <div>{
       {
        message }}</div>
      </div>
    </template>
     
     
     
    <script setup lang="ts">
    import {
        Ref, shallowRef } from 'vue'
    type Obj = {
       
      name: string
    }
    let message: Ref<Obj> = shallowRef({
       
      name: "小满"
    })
     
    const changeMsg = () => {
       
      message.value.name = '大满' // 这样修改无法改变
    }
    
    const changeMsg = () => {
       
      message.value = {
        name: "大满" } // 修改他的 value 才会被监听到 从而发送改变
    }
    </script>
     
    <style>
    </style>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    5. triggerRef

    强制更新页面DOM

    这样也是可以改变值的 (可以使shallowRef的值强制刷新,从而刷新dom )

    <template>
      <div>
        <button @click="changeMsg">change</button>
        <div>{
       {
        message }}</div>
      </div>
    </template>
     
    <script setup lang="ts">
    import {
        Ref, shallowRef,triggerRef } from 'vue'
    type Obj = {
       
      name: string
    }
    let message: Ref<Obj> = shallowRef({
       
      name: "小满"
    })
     
    const changeMsg = () => {
       
      message.value.name = '大满'
     triggerRef(message)
    }
    </script> 
     
     
    <style>
    </style>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    6. customRef 和 ref 直接获取dom元素

    自定义ref

    customRef 是个工厂函数要求我们返回一个对象 并且实现 get 和 set 适合去做防抖之类的

    <template>
    
     <div ref="dom">   // ref 可以获取dom元素  ,但是 ref = "dom"
        wsh
     </div>
    <hr>
    <div>
      {
       {
       name}}
    </div>
    <hr>
    <button @click="change">修改</button>
    </template>
    
    <script setup lang='ts'>
    import {
       ref, reactive, onMounted, shallowRef, customRef} from 'vue'
    
    function myRef<T>(value: T) {
       
      let timer:any;
      return customRef((track, trigger) => {
       
        return{
       
          get() {
       
            track()
            return value
          },
          set(newVal) {
       
            clearTimeout(timer)
            timer = setTimeout(() => {
       
              console.log('dianjiset')
              value =
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
  • 相关阅读:
    Java时间Api
    双十一特辑-北汇在C站的两周年打卡纪念:)
    二、nacos注册中心配置与应用
    openSuSE 下面配置 java环境变量
    【Linux】之shell入门
    HashMap总结
    Java Spring Boot----ruoyi项目部署 前后端分离
    大规模语言模型人类反馈对齐--RLHF
    好用的数据恢复软件EasyRecovery2023最新版
    SpringCloud Alibaba微服务第5章之项目初始化
  • 原文地址:https://blog.csdn.net/m0_55170432/article/details/127653343