vue3快速入门

<template>
  <h1>{{ count }}</h1>
  <h1>{{ myData }}</h1>
  <h1>{{ content }}</h1>
  <h1>{{ getLen }}</h1>
  <h1>{{ getLen }}</h1>
  <button @click="clickfn">修改</button>
</template>
<script setup>
import { shallowReactive,ref,computed,watch,watchEffect} from 'vue';

// 1、reactive 全响应式 必须是对象类型 | shallowReactive 浅响应式  只有第一层属性是响应式
const myData = shallowReactive({
  name:'szx',
  friends:[1,2,3]
})

// 2、ref .value
const count = ref(0)

// 3、readonly 不能改 | shadowReadonly 只有根属性只读

function clickfn(){
  myData.friends.push(10)
  myData.name+=' ! '
  count.value++
  content.value += '!'
}

// 4、computed 虽然有两次进行渲染 但是点击 每次只会修改一个值 
// 因为只要数据有了变化 计算出数据结果后就缓存 
// 第一次修改 后面没修改就直接使用缓存结果 避免重复执行
// 这是计算属性执行的一个基本特征
const content = ref('这是一段测试内容')
const getLen = computed(()=>{
  console.log('计算属性执行了')
  return content.value.length
})

// 5、watch 侦听单一数据变化
// 对ref
watch(count,(newValue,oldValue)=>{
  console.log(newValue,oldValue)
})
// 对reactive 第一个参数需要对属性进行包裹
watch(() => myData.name,(newValue,oldValue)=>{
  console.log(newValue,oldValue)
})

// 6、watchEffect 副作用函数 不用写监听对象 自动监听函数内的 
// 多个响应式数据
watchEffect(()=>{
  console.log(count.value,myData.name)
})
</script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

prominent.949

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值