美文网首页
Vue3_09(认识computed计算属性)

Vue3_09(认识computed计算属性)

作者: BingJS | 来源:发表于2022-05-30 18:14 被阅读0次

computed用法

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

1. 函数形式

import { computed, reactive, ref } from 'vue'
let price = ref(0)//$0
 
let m = computed<string>(()=>{
   return `$` + price.value
})
 
price.value = 500

2. 对象形式

<template>
   <div>{{ mul }}</div>
   <div @click="mul = 100">click</div>
</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>

computed购物车案例

<template>
   <div>
      <table style="width:800px" border>
         <thead>
            <tr>
               <th>名称</th>
               <th>数量</th>
               <th>价格</th>
               <th>操作</th>
            </tr>
         </thead>
         <tbody>
            <tr :key="index" v-for="(item, index) in data">
               <td align="center">{{ item.name }}</td>
               <td align="center">
                  <button @click="AddAnbSub(item, false)">-</button>
                  {{ item.num }}
                  <button @click="AddAnbSub(item, true)">+</button>
               </td>
               <td align="center">{{ item.num * item.price }}</td>
               <td align="center">
                  <button @click="del(index)">删除</button>
               </td>
            </tr>
         </tbody>
         <tfoot>
            <tr>
               <td></td>
               <td></td>
               <td></td>
               <td align="center">总价:{{ $total }}</td>
            </tr>
         </tfoot>
      </table>
   </div>
</template>
 
<script setup lang="ts">
import { computed, reactive, ref } from 'vue'
type Shop = {
   name: string,
   num: number,
   price: number
}
let $total = ref<number>(0)
const data = reactive<Shop[]>([
   {
      name: "袜子",
      num: 1,
      price: 100
   },
   {
      name: "裤子",
      num: 1,
      price: 200
   },
   {
      name: "衣服",
      num: 1,
      price: 300
   },
   {
      name: "毛巾",
      num: 1,
      price: 400
   }
])
 
const AddAnbSub = (item: Shop, type: boolean = false): void => {
   if (item.num > 1 && !type) {
      item.num--
   }
   if (item.num <= 99 && type) {
      item.num++
   }
}
const del = (index: number) => {
   data.splice(index, 1)
}
 
$total = computed<number>(() => {
   return data.reduce((prev, next) => {
      return prev + (next.num * next.price)
   }, 0)
})
</script>
 
<style>
</style>

相关文章

  • Vue3_09(认识computed计算属性)

    computed用法 计算属性就是当依赖的属性的值发生变化的时候,才会触发他的更改,如果依赖的值,不发生变化的时候...

  • 计算属性

    1.计算属性get方法: 计算属性(computed)和Methods区别:计算属性(computed)适合:有缓...

  • Vue复习

    Vue的计算属性 计算属性computed

  • 03.vue3-组合API(下篇)

    组合API-computed函数 定义计算属性: computed函数,是用来定义计算属性的,计算属性不能修改。基...

  • 监听器和计算属性的区别watch,computed

    计算属性computed和监听器watch区别?1.能使用计算属性computed的尽量使用计算属性,但是计算属性...

  • Vue之计算属性computed(一)

    Vue中什么是计算属性computed,计算属性的基础、计算属性computed与方法method实现相同的功能为...

  • 3.vue计算属性和过滤器

    1.计算属性 Vue中的computed属性称为计算属性.它与methods不同,computed是响应式的,调用...

  • Vue

    computed 计算属性 computed的结果会被缓存,除非依赖的响应式属性变化才会重新计算,主要当做属性来使...

  • computed缓存 VS methods方法

    computed 计算属性【选项】 computed 属性会基于它所依赖的数据进行缓存 每个 computed 属...

  • computed && watch && methods

    computed计算属性适用于任何复杂逻辑的计算 computed 和 计算方法computed: 是基于它们的依...

网友评论

      本文标题:Vue3_09(认识computed计算属性)

      本文链接:https://www.haomeiwen.com/subject/txjsdrtx.html