国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

【Vue3】computed 計(jì)算屬性

這篇具有很好參考價(jià)值的文章主要介紹了【Vue3】computed 計(jì)算屬性。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

computed 是計(jì)算屬性,它會(huì)根據(jù)響應(yīng)式數(shù)據(jù)的變化?動(dòng)計(jì)算出新的值,并緩存結(jié)果,只有在計(jì)算屬性所依賴的響應(yīng)式數(shù)據(jù)發(fā)?改變時(shí)才會(huì)重新計(jì)算。

computed 適?于需要根據(jù)響應(yīng)式數(shù)據(jù)計(jì)算得出結(jié)果的場(chǎng)景,例如根據(jù)商品的數(shù)量和單價(jià)計(jì)算商品的總價(jià),或者根據(jù)選中的過(guò)濾條件過(guò)濾出數(shù)據(jù)列表。

computed 基礎(chǔ)語(yǔ)法

<template>
  <div>
    <div>
      性:<input v-model="firstName" type="text">
    </div>
    <div>
      名:<input v-model="lastName" type="text">
    </div>
    <div>
      全名:<input v-model="fullName" type="text">
    </div>
    <button @click="changeName">changeName</button>
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue';
let firstName = ref('zhang')
let lastName = ref('san')

// 1.選項(xiàng)式寫法 支持一個(gè)對(duì)象傳入getter和setter自定義操作
let fullName = computed<string>({
  get () {
    return firstName.value + '-' + lastName.value
  },
  set(newVal) {
  	// newVal.split('-') 此時(shí)沒(méi)有 .value
    console.log(newVal.split('-'));
    // 
    [firstName.value,lastName.value] = newVal.split('-')
  }
})
const changeName = () => [
  fullName.value = 'xiao-hu'
]


//2.函數(shù)式寫法,只能支持一個(gè)getter函數(shù),不可以修改值
// let fullName = computed(() => firstName.value + '-' + lastName.value)
// 不可以修改,以下這個(gè)函數(shù)會(huì)報(bào)錯(cuò)
// const changeName = () => {
//   fullName.value = 'xiao-hu'
// }
</script>

<style lang="scss" scoped></style>

購(gòu)物車小案例

未使用computed優(yōu)化

<template>
  <div>
    <input placeholder="搜索" type="text">
  </div>
  <div style="margin-top: 20px">
    <table border width="600" cellpadding="0" cellspacing="0">
      <thead>
        <tr>
          <th>物品名稱:</th>
          <th>物品單價(jià):</th>
          <th>物品數(shù)量:</th>
          <th>物品總價(jià):</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(item,index) in data">
          <td align="center">{{ item.name }}</td>
          <td align="center">{{ item.price }}</td>
          <td align="center"><button @click="item.num>1 ? (item.num--, total()) : null">-</button>{{ item.num }}<button @click="item.num < 99 ? (item.num++, total()) : null">+</button></td>
          <td align="center">{{ item.num * item.price }}</td>
          <td align="center"><button @click="del(index)">del</button></td>
        </tr>
      </tbody>
      <tfoot>
        <tr>
          <td colspan="5" align="right">
            總價(jià):{{ $total }}
          </td>
          
        </tr>
      </tfoot>
    </table>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
let $total = ref<number>(0)
interface Data {
  name: string,
  price: number,
  num: number,
}
let data = reactive<Data[]>([
  {
    name: '手機(jī)',
    price: 100,
    num: 1
  },
  {
    name: '電腦',
    price: 200,
    num: 2
  },
  {
    name: '筆記本',
    price: 300,
    num: 3
  }
])

const total = () => {
  $total.value = data.reduce((prev:number, next: Data) => {
    return prev + next.num * next.price
  },0)
}
total()
const del = (index:number) => {
  data.splice(index, 1)
  total()

}
</script>

<style lang="scss" scoped></style>

使用computed優(yōu)化

<template>
  <div>
    <input v-model="keyWord" placeholder="搜索" type="text">
  </div>
  <div style="margin-top: 20px">
    <table border width="600" cellpadding="0" cellspacing="0">
      <thead>
        <tr>
          <th>物品名稱:</th>
          <th>物品單價(jià):</th>
          <th>物品數(shù)量:</th>
          <th>物品總價(jià):</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(item, index) in searchData">
          <td align="center">{{ item.name }}</td>
          <td align="center">{{ item.price }}</td>
          <td align="center"><button @click="item.num > 1 ? item.num-- : null">-</button>{{ item.num }}<button
              @click="item.num < 99 ? item.num++ : null">+</button></td>
          <td align="center">{{ item.num * item.price }}</td>
          <td align="center"><button @click="del(index)">del</button></td>
        </tr>
      </tbody>
      <tfoot>
        <tr>
          <td colspan="5" align="right">
            總價(jià):{{ total }}
          </td>

        </tr>
      </tfoot>
    </table>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, computed } from 'vue'
let keyWord = ref<string>('')
interface Data {
  name: string,
  price: number,
  num: number,
}
let data = reactive<Data[]>([
  {
    name: '手機(jī)',
    price: 100,
    num: 1
  },
  {
    name: '電腦',
    price: 200,
    num: 2
  },
  {
    name: '筆記本',
    price: 300,
    num: 3
  }
])

// const total = () => {
//   $total.value = data.reduce((prev:number, next: Data) => {
//     return prev + next.num * next.price
//   },0)
// }
const total = computed(() => {
  return data.reduce((prev: number, next: Data) => {
    return prev + next.num * next.price
  }, 0)
})

const searchData = computed(() => {
  return data.filter((item: Data) => {
    return item.name.includes(keyWord.value)
  })
})
// 使用 searchData 搜索后的總價(jià)有bug,不會(huì)隨 searchData 的數(shù)據(jù)改變(抽空再改啦~~)

const del = (index: number) => {
  data.splice(index, 1)
}
</script>

<style lang="scss" scoped></style>

watch 和 computed 的區(qū)別:

計(jì)算屬性 computed :

  1. 支持緩存,只有依賴數(shù)據(jù)發(fā)生改變,才會(huì)重新進(jìn)行計(jì)算
  2. 不支持異步,當(dāng)computed內(nèi)有異步操作時(shí)無(wú)效,無(wú)法監(jiān)聽數(shù)據(jù)的變化
  3. computed 屬性值會(huì)默認(rèn)走緩存,計(jì)算屬性是基于它們的響應(yīng)式依賴進(jìn)行緩存的,也就是基于data中聲明過(guò)或者父組件傳遞的props中的數(shù)據(jù)通過(guò)計(jì)算得到的值
  4. 如果一個(gè)屬性是由其他屬性計(jì)算而來(lái)的,這個(gè)屬性依賴其他屬性,是一個(gè)多對(duì)一或者一對(duì)一,一般用computed
  5. 如果computed屬性屬性值是函數(shù),那么默認(rèn)會(huì)走get方法;函數(shù)的返回值就是屬性的屬性值;在computed中的,屬性都有一個(gè)get和一個(gè)set方法,當(dāng)數(shù)據(jù)變化時(shí),調(diào)用set方法。
computed: {
    // 一個(gè)計(jì)算屬性的 getter
    publishedBooksMessage() {
      // `this` 指向當(dāng)前組件實(shí)例
      return this.author.books.length > 0 ? 'Yes' : 'No'
    }
  }

偵聽屬性watch:文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-605613.html

  1. 不支持緩存,數(shù)據(jù)變,直接會(huì)觸發(fā)相應(yīng)的操作;
  2. watch支持異步;
  3. 監(jiān)聽的函數(shù)接收兩個(gè)參數(shù),第一個(gè)參數(shù)是最新的值;第二個(gè)參數(shù)是輸入之前的值;
  4. 當(dāng)一個(gè)屬性發(fā)生變化時(shí),需要執(zhí)行對(duì)應(yīng)的操作;一對(duì)多;
  5. 監(jiān)聽數(shù)據(jù)必須是data中聲明過(guò)或者父組件傳遞過(guò)來(lái)的props中的數(shù)據(jù),當(dāng)數(shù)據(jù)變化時(shí),觸發(fā)其他操作,函數(shù)有兩個(gè)參數(shù),
    • immediate:組件加載立即觸發(fā)回調(diào)函數(shù)執(zhí)行,
    • deep: 深度監(jiān)聽,為了發(fā)現(xiàn)對(duì)象內(nèi)部值的變化,復(fù)雜類型的數(shù)據(jù)時(shí)使用,例如數(shù)組中的對(duì)象內(nèi)容的改變,注意監(jiān)聽數(shù)組的變動(dòng)不需要這么做。
watch: {
    // 每當(dāng) question 改變時(shí),這個(gè)函數(shù)就會(huì)執(zhí)行
    question(newQuestion, oldQuestion) {
      if (newQuestion.includes('?')) {
        this.getAnswer()
      }
    }
  },
watch: {
    someObject: {
      handler(newValue, oldValue) {
        // 注意:在嵌套的變更中,
        // 只要沒(méi)有替換對(duì)象本身,
        // 那么這里的 `newValue` 和 `oldValue` 相同
      },
      deep: true
    }
  }

到了這里,關(guān)于【Vue3】computed 計(jì)算屬性的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • vue全家桶進(jìn)階之路33:Vue3 計(jì)算屬性computed

    在Vue3中,計(jì)算屬性可以使用 computed 函數(shù)來(lái)定義。 computed 函數(shù)接受兩個(gè)參數(shù):第一個(gè)參數(shù)是一個(gè)函數(shù),該函數(shù)返回計(jì)算屬性的值;第二個(gè)參數(shù)是一個(gè)可選的配置對(duì)象,可以包含getter和setter函數(shù),以及控制計(jì)算屬性緩存的緩存配置。 Vue3中的計(jì)算屬性與Vue2中的計(jì)算屬性相比有以

    2023年04月18日
    瀏覽(20)
  • 【源碼系列#03】Vue3計(jì)算屬性原理(Computed)

    【源碼系列#03】Vue3計(jì)算屬性原理(Computed)

    專欄分享:vue2源碼專欄,vue3源碼專欄,vue router源碼專欄,玩具項(xiàng)目專欄,硬核??推薦?? 歡迎各位ITer關(guān)注點(diǎn)贊收藏?????? 傳入一個(gè) getter 函數(shù),返回一個(gè)默認(rèn)不可手動(dòng)修改的 ref 對(duì)象 或者傳入一個(gè)擁有 get 和 set 函數(shù)的對(duì)象,創(chuàng)建一個(gè)可手動(dòng)修改的計(jì)算狀態(tài) @issue1 compute

    2024年02月05日
    瀏覽(51)
  • vue3 源碼解析(3)— computed 計(jì)算屬性的實(shí)現(xiàn)

    本文是 vue3 源碼分析系列的第三篇文章,主要介紹 vue3 computed 原理。computed 是 vue3 的一個(gè)特性,可以根據(jù)其他響應(yīng)式數(shù)據(jù)創(chuàng)建響應(yīng)式的計(jì)算屬性。計(jì)算屬性的值會(huì)根據(jù)依賴的數(shù)據(jù)變化而自動(dòng)更新,而且具有緩存機(jī)制,提高了性能。在這篇文章中,我們將深入探討 computed 的實(shí)現(xiàn)

    2024年01月16日
    瀏覽(26)
  • Vue3的computed計(jì)算屬性和watch監(jiān)視(四)

    Vue3的computed計(jì)算屬性和watch監(jiān)視(四)

    監(jiān)視【ref】定義的【基本數(shù)據(jù)】類型 監(jiān)視【ref】定義的【對(duì)象類型】數(shù)據(jù) 監(jiān)視【reactive】定義的【對(duì)象類型】數(shù)據(jù) ?與 場(chǎng)景二 不同的是,newVal和oldVal是一樣的,表明通過(guò)Object.assign重新賦值的時(shí)候,并不是生成一個(gè)新的對(duì)象,而是新的值覆蓋了舊值 監(jiān)視【ref】或者【reactiv

    2024年02月21日
    瀏覽(23)
  • 前端Vue入門-day02-vue指令、computed計(jì)算屬性與watch偵聽器

    前端Vue入門-day02-vue指令、computed計(jì)算屬性與watch偵聽器

    (創(chuàng)作不易,感謝有你,你的支持,就是我前行的最大動(dòng)力,如果看完對(duì)你有幫助,請(qǐng)留下您的足跡) 目錄 指令補(bǔ)充 指令修飾符 v-bind 對(duì)于樣式控制的增強(qiáng)? 操作class 案例:京東秒殺 tab 導(dǎo)航高亮 操作style? v-model 應(yīng)用于其他表單元素? computed 計(jì)算屬性 基礎(chǔ)語(yǔ)法 computed 計(jì)算屬

    2024年02月11日
    瀏覽(53)
  • Vue3 計(jì)算屬性和偵聽器實(shí)戰(zhàn)(computed、watch)——簡(jiǎn)易點(diǎn)餐頁(yè)面

    Vue3 計(jì)算屬性和偵聽器實(shí)戰(zhàn)(computed、watch)——簡(jiǎn)易點(diǎn)餐頁(yè)面

    這篇文章記錄一下 Vue3 計(jì)算屬性和偵聽器 (computed、watch) 實(shí)戰(zhàn)的內(nèi)容,這篇文章我們?cè)谟杏?jì)算屬性和偵聽器的基礎(chǔ)上,我們來(lái)制作一個(gè)簡(jiǎn)易點(diǎn)餐頁(yè)面,接下來(lái)我們一起來(lái)從零到一開始制作。 計(jì)算屬性和偵聽器相關(guān)文章推薦: 深入與淺談 Vue 中計(jì)算屬性和偵聽器的區(qū)別和使用

    2024年02月09日
    瀏覽(34)
  • GuLi商城-前端基礎(chǔ)Vue-計(jì)算屬性computed和偵聽器watch
  • Vue計(jì)算屬性Computed傳參

    Vue計(jì)算屬性Computed傳參

    關(guān)于computed計(jì)算屬性傳參的問(wèn)題,因?yàn)閏omputed是計(jì)算屬性,如果給conputed傳參則會(huì)直接報(bào)錯(cuò),并且報(bào)computed is not function。 解決辦法: 方法一: 通過(guò)返回函數(shù)來(lái)進(jìn)行傳參: 代碼: 分析: 既然計(jì)算屬性不能做函數(shù)一樣進(jìn)行傳參,但是computed有一個(gè) return 我們可以利用起來(lái),所以我

    2024年02月16日
    瀏覽(12)
  • vue 計(jì)算屬性未重新計(jì)算 / computed 未觸發(fā) / computed 原理&源碼分析

    vue 計(jì)算屬性未重新計(jì)算 / computed 未觸發(fā) / computed 原理&源碼分析

    點(diǎn)擊可打開demo 這里在一秒后改了數(shù)組里value屬性的值 雖然數(shù)據(jù)有更新,但打開控制臺(tái),可以發(fā)現(xiàn)computed函數(shù)只在初始化時(shí)執(zhí)行了一次 按理說(shuō)一秒后改變了value值,應(yīng)該執(zhí)行兩次才對(duì)呀? 但如果computed屬性這樣寫,明確寫明展開了每一項(xiàng),獲取到了value屬性,就能執(zhí)行第二次

    2024年02月06日
    瀏覽(20)
  • Vue-計(jì)算屬性(computed)簡(jiǎn)單說(shuō)明和使用

    Vue-計(jì)算屬性(computed)簡(jiǎn)單說(shuō)明和使用

    學(xué)習(xí)vue的計(jì)算屬性之前,我們先寫一個(gè)案例,我們先用插值語(yǔ)法實(shí)現(xiàn),然后再使用vue的計(jì)算屬性實(shí)現(xiàn),經(jīng)過(guò)對(duì)比,我們就能掌握計(jì)算屬性的精髓和原理 寫一個(gè)簡(jiǎn)單的例子,姓和名分別用兩個(gè)輸入框控制,最后通過(guò)一個(gè)span標(biāo)簽拼接成一個(gè)全名 首先通過(guò)簡(jiǎn)單的插值語(yǔ)法實(shí)現(xiàn),需

    2024年01月16日
    瀏覽(15)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包