[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
<>写在前面
目标:现在vue3的使用越来越普遍了,vue3这方面的学习我们要赶上,今天说一下vue3中computed计算属性的使用。
computed函数在vue2中我们都了解过,当我们需要一个值,这个值随着其他值变化而变化,我们就将其放进computed中,computed是用来定义计算属性的。
<>1. 基本使用:只读
<template> <div> <div>计算属性</div> <hr> <button @click='age=25'>点击</button>
<div>今年:{{age}}岁了</div> <div>明年:{{nextAge}}岁了</div> </div> </template> <script>
import { ref, computed } from 'vue' export default { name: 'App', setup () { //
计算属性:简化模板 // 应用场景:基于已有的数据,计算一种数据 const age = ref(18) // 计算属性基本使用 const nextAge
= computed(() => { // 回调函数必须return,结果就是计算的结果 // 如果计算属性依赖的数据发生变化,那么会重新计算 return
age.value + 1 }) return { age, nextAge } } } </script> <style lang="less">
</style>
总结:Vue3中计算属性也是组合API风格
* 回调函数必须return,结果就是计算的结果
* 如果计算属性依赖的数据发生变化,那么会重新计算
* 不要在计算中中进行异步操作
<>2 高级用法:可读可写
<template> <div> <div>计算属性</div> <hr> <button @click='age=25'>点击</button>
<button @click='nextAge=28'>点击修改</button> <div>今年:{{age}}岁了</div> <div>明年:{
{nextAge}}岁了</div> </div> </template> <script> import { ref, computed } from
'vue' export default { name: 'App', setup () { // 计算属性:简化模板 //
应用场景:基于已有的数据,计算一种数据 const age = ref(18) // // 计算属性基本使用 // const nextAge =
computed(() => { // // 回调函数必须return,结果就是计算的结果 // // 如果计算属性依赖的数据发生变化,那么会重新计算 //
return age.value + 1 // }) // 修改计算属性的值 const nextAge = computed({ get () { //
如果读取计算属性的值,默认调用get方法 return age.value + 1 }, set (v) { // v是计算属性下传递的实参 //
如果要想修改计算属性的值,默认调用set方法 age.value = v - 1 } }) return { age, nextAge } } }
</script> <style lang="less"> </style>
总结:
* 计算属性可以直接读取或者修改
* 如果要实现计算属性的修改操作,那么computed的实参应该是对象
* 读取数据触发get方法
* 修改数据触发set方法,set函数的形参就是你赋给他的值