首页 > 其他分享 >学习Vue3 第六章(认识Ref全家桶)

学习Vue3 第六章(认识Ref全家桶)

时间:2023-12-05 13:44:36浏览次数:35  
标签:const name Ref value Vue3 第六章 message ref

  ref

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

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

    isRef

    判断是不是一个ref对象

import { ref, Ref,isRef } from 'vue'
let message: Ref<string | number> = ref("我是message")
let notRef:number = 123
const changeMsg = () => {
  message.value = "change msg"
  console.log(isRef(message)); //true
  console.log(isRef(notRef)); //false
  
}

 

    shallowRef

    创建一个跟踪自身 .value 变化的 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 = '大满'
}
</script>
 
 
<style>
</style>

  triggerRef 

    强制更新页面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>

  customRef

    自定义ref 

<template>
 
  <div ref="div">小满Ref</div>
  <hr>
  <div>
    {{ name }}
  </div>
  <hr>
  <button @click="change">修改 customRef</button>
 
</template>
 
<script setup lang='ts'>
import { ref, reactive, onMounted, shallowRef, customRef } from 'vue'
 
function myRef<T = any>(value: T) {
  let timer:any;
  return customRef((track, trigger) => {
    return {
      get() {
        track()
        return value
      },
      set(newVal) {
        clearTimeout(timer)
        timer =  setTimeout(() => {
          console.log('触发了set')
          value = newVal
          trigger()
        },500)
      }
    }
  })
}
 
 
const name = myRef<string>('小满')
 
 
const change = () => {
  name.value = '大满'
}
 
</script>
<style scoped>
</style>

 

标签:const,name,Ref,value,Vue3,第六章,message,ref
From: https://www.cnblogs.com/liziqian001/p/17877020.html

相关文章

  • Vite4+Typescript+Vue3+Pinia 从零搭建(6) - 状态管理pina
    项目代码同步至码云weiz-vue3-templatepina是vue3官方推荐的状态管理库,由Vue核心团队维护,旨在替代vuex。pina的更多介绍,可从pina官网查看特点更简洁直接的API,提供组合式风格的API支持模块热更新和服务端渲染对TS支持更为友好安装npmipinia使用1.创建......
  • uniapp+vue3 优惠券样式
    效果如图:template部分:<viewclass="item"><viewclass="box"><viewclass="content"><viewclass="head">优惠券</view><viewclass="content-box1">......
  • Vue3 实现网页水印
    一些公司和组织出于系统文件或信息安全保密的需要,需要在系统网页上增加带有个人标识的水印。首先我们来看这样一个水印功能的实现思路,通常是在我们原有的网页上附上一个DIV层,将它设置绝对定位铺满整个窗口,然后z-index值尽量往大了设,保证让水印层处于当前网页所有元素的上面,又不......
  • vue3 setup 父组件向子组件传递参数、方法|子组件向父组件传递数据,函数
    https://blog.csdn.net/qq_27517377/article/details/123163381https://blog.csdn.net/qq_27517377/article/details/123166367vue3setup父组件向子组件传递参数参数<template><el-rowclass="mb-4"> <el-buttontype="danger">props.vue传......
  • echarts:ReferenceError: echarts is not defined
    echarts资源引用正常,但却出现定义报错。原因:引入echarts.js位置不正确,导致未引入js时,却使用了echarts。解决:先引入echarts.js再使用建议将引入放在body后,否则可能会出现>TypeError:Cannotreadpropertiesofnull(reading'getAttribute')......
  • 手写类似于BetterScroll样式的左右联动菜单 uni-app+vue3+ts (使用了script setup语法
     注意:在模拟器用鼠标滚动是不会切换光标的,因为使用的是触摸滑动。【自定义类型贴在最后了】script部分如下:import{onMounted}from'vue'importtype{orderDetail}from'@/types/category'importtype{mainArr}from'@/types/main-arr'import{nextTick,ref}......
  • vue3使用::v-deep深度选择器不生效
    会出现 ::v-deepusageasacombinatorhasbeendeprecated.Use:deep(<inner-selector>)insteadof::v-deep<inner-selector>.的报错::v-depth用作组合子已被弃用。使用:deep(<内部选择器>)而不是::v-deep<内部选择器>。需要改成:deep(class),示例代码如下:deep(.el-checkbo......
  • 2023ICCV_Feature Modulation Transformer: Cross-Refinement of Global Representati
    一.Motivation1.transformer的工作主要集中在设计transformer块以获得全局信息,而忽略了合并高频先验的潜力2. 关于频率对性能的影响的详细分析有限(Additionally,there islimiteddetailedanalysisoftheimpactoffrequencyon performance.)注: (1) 图说明:随着高......
  • Vue3用户代码片段
    1.defineComponent语法{ "Printtoconsole":{ "prefix":"vue3", "body":[ "<template>", "<divclass=\"container\">", "", "</div>&q......
  • Docker本地部署私人Firefox火狐浏览器并远程访问(宝藏教程)
    Firefox是一款免费开源的网页浏览器,由Mozilla基金会开发和维护。它是第一个成功挑战微软InternetExplorer浏览器垄断地位的浏览器之一。在Docker中打开Firefox意味着我们将在一个Docker容器中运行Firefox浏览器。这对于一些特殊的测试场景非常有用,例如需要在不同版本的浏览器中......