首页 > 其他分享 >学习Vue3 (三)

学习Vue3 (三)

时间:2022-11-01 20:24:56浏览次数:64  
标签:console log watch 学习 let Vue3 message ref

computed用法

计算属性就是当依赖的属性的值发生变化的时候,才会触发他的更改,如果依赖的值,不发生变化的时候,使用的是缓存中的属性值。

1 函数形式

<template>
  <div>
    <h1>{{ m }}</h1>
  </div>
</template>
 
<script setup lang="ts">
import { computed, reactive, ref } from 'vue'
let price = ref(0)//$0
 
let m = computed<string>(()=>{
   return `$` + price.value
})
 
price.value = 500

</script>

2 对象形式 

<template>
   <div>{{ mul }}</div>
   <button @click="mul = 100">click</button>
</template>
 
<script setup lang="ts">
import { computed, ref } from 'vue'
let price = ref<number | string>(1)//$0
let mul = computed({
   get: () => {
      return price.value
   },
   set: (value) => {
      price.value = 'set' + value
   }
})
</script>
 
<style>
</style>

watch侦听器

watch第一个参数监听源

watch第二个参数回调函数cb(newVal,oldVal)

watch第三个参数一个options配置项是一个对象{

immediate:true //是否立即调用一次

deep:true //是否开启深度监听

}

<template>
   <div>{{ message }}</div>
</template>
 
<script setup lang="ts">
import { ref, watch } from 'vue'
 
let message = ref({
    nav:{
        bar:{
            name:""
        }
    }
})
 
 
watch(message, (newVal, oldVal) => {
    console.log('新的值----', newVal);
    console.log('旧的值----', oldVal);
},{
    immediate:true, //是否立即调用一次
    deep:true //是否开启深度监听
})
</script>
 
<style>
</style>

监听多个ref 注意变成数组啦  

<template>
   <div>{{ message }}</div>
</template>
 
<script setup lang="ts">
import { ref, watch ,reactive} from 'vue'
 
let message = ref('')
let message2 = ref('')
 
watch([message,message2], (newVal, oldVal) => {
    console.log('新的值----', newVal);
    console.log('旧的值----', oldVal);
})
</script>
 
<style>
</style>

监听Reactive

使用reactive监听深层对象开启和不开启deep 效果一样

<template>
   <div>{{ message }}</div>
</template>
 
<script setup lang="ts">
import { ref, watch ,reactive} from 'vue'
 
let message = reactive({
    nav:{
        bar:{
            name:""
        }
    }
})
 
 
watch(message, (newVal, oldVal) => {
    console.log('新的值----', newVal);
    console.log('旧的值----', oldVal);
},{
    immediate:true,
    deep:true
})
</script>
 
<style>
</style>

案例2 监听reactive 单一值  

<template>
   <div>{{ message }}</div>
</template>
 
<script setup lang="ts">
import { ref, watch ,reactive} from 'vue'
 
let message = reactive({
    name:"",
    name2:""
})
 
 
watch(()=>message.name, (newVal, oldVal) => {
    console.log('新的值----', newVal);
    console.log('旧的值----', oldVal);
})
</script>
 
<style>
</style>

认识watchEffect高级侦听器

立即执行传入的一个函数,同时响应式追踪其依赖,并在其依赖变更时重新运行该函数。

如果用到message 就只会监听message 就是用到几个监听几个 而且是非惰性 会默认调用一次

<template>
   <div>{{ message }}</div>
</template>
 
<script setup lang="ts">
import { ref, watch ,reactive,watchEffect} from 'vue'
 
let message = ref<string>('')
let message2 = ref<string>('')
 watchEffect(() => {
    //console.log('message', message.value);
    console.log('message2', message2.value);
})
</script>
 
<style>
</style>

清除副作用

就是在触发监听之前会调用一个函数可以处理你的逻辑例如防抖

<template>
   <div>
     <input type="text" v-model="message">
   </div>
</template>
 
<script setup lang="ts">
import { watchEffect, ref } from 'vue'
let message = ref<string>('')
let message2 = ref<string>('')
 watchEffect((oninvalidate) => {
    console.log('message', message.value);
    oninvalidate(()=>{
        console.log("before")
    })
})
</script>
 
<style>
</style>

停止跟踪 watchEffect 返回一个函数 调用之后将停止更新  

<template>
   <div>
     <input type="text" v-model="message">
   </div>
</template>
 
<script setup lang="ts">
import { watchEffect, ref } from 'vue'
let message = ref<string>('')
let message2 = ref<string>('')
const stop =  watchEffect((oninvalidate) => {
    //console.log('message', message.value);
    oninvalidate(()=>{
      console.log("before")
    })
    console.log('message2', message2.value);
},{
    flush:"post",
    onTrigger () {
      console.log("stop")
    }
})
stop()
</script>
 
<style>
</style>

  

  

  

  

  

 

标签:console,log,watch,学习,let,Vue3,message,ref
From: https://www.cnblogs.com/xiaobaibubai/p/16849003.html

相关文章

  • Maven学习1
    一、Maven是什么呢1.Maven作为Apache组织中的一个颇为成功的开源项目,主要服务于基于java平台的项目构建,依赖管理和项目信息管理。2.项目对象模型,通过其描述信息来管理项......
  • 生成新冠疫苗接种趋势图-技术和法律学习
    文档说明:只记录关键地方;缘由:想看疫苗接种趋势图,我要去哪里找呢?奈何公开的渠道没有找到!自己做一个呗数据来源:http://www.nhc.gov.cn/xcs/xxgzbd/gzbd_index.shtml(......
  • 关于Markdown语法的入门学习与使用
    Markdown介绍(百度扒的)Markdown是一种轻量级标记语言,创始人为约翰·格鲁伯(JohnGruber)。它允许人们使用易读易写的纯文本格式编写文档,然后转换成有效的XHTML(或者HTML)文......
  • 学习vue3(二)
    reactive用来绑定复杂的数据类型例如对象数组,他是不可以绑定普通的数据类型这样是不允许会报错绑定普通的数据类型我们可以使用昨天讲到ref你如果用ref去绑定对象......
  • R机器学习:特征工程与特征选择的介绍
    两个月没更新了,没有其它理由,就是懒惰,间接持续性的懒惰,一直持续了2个月,简直懒惰!!!大家的好多的私信也没回就过期回不了了。请大家批评我!!!。看了很多高深的算法之后,实在是看不......
  • hadoop学习(2)
    HadoopYARN直接源于MRv1在几个方面的缺陷,扩展性受限、单点故障、难以支持MR之外的计算。多计算框架各自为战,数据共享困难。MR:离线计算框架,Storm:实时计算框架,Spark内存计......
  • 20201306吴龙灿第十二章学习笔记
    知识点归纳1.块设备I/O缓冲区什么是块设备:块设备是i/o设备中的一类,是将信息存储在固定大小的块中,每个块都有自己的地址,还可以在设备的任意位置读取一定长度的数据,例如硬......
  • Java学习笔记day2--循环结构
    **循环结构包括四个部分:初始化部分循环条件部分循环体部分迭代部分1>for循环for(初始化部分;循环条件部分;迭代条件......
  • 2022-10-31学习内容
    1.数据库版本升级1.1UserDBHelper.javaprivatestaticfinalintDB_VERSION=2;@OverridepublicvoidonUpgrade(SQLiteDatabasedb,intoldVersion,intnew......
  • biogeme巢式logit选择学习
    #导入库```pythonimportpandasaspdimportcsvfrombiogemeimportmodelsimportbiogeme.biogemeasbioimportbiogeme.databaseasdbfrombiogeme.expressionsimpo......