前端vue3中的ref與reactive用法及區(qū)別總結(jié)
ref與reactive的區(qū)別
接收值的類型不同
- ref:
ref
可以處理基礎(chǔ)類型的值,也可以處理引用類型的值。
const ref1 = ref(0) // true const ref2 = ref({ count: 0 }) // true
用于創(chuàng)建一個響應(yīng)式的基本數(shù)據(jù)類型時,比如數(shù)字、字符串等。它將普通的數(shù)據(jù)變成響應(yīng)式數(shù)據(jù),可以監(jiān)聽數(shù)據(jù)的變化。使用ref
時,我們可以通過.value
來訪問和修改數(shù)據(jù)的值。
- reactive:
reactive
僅處理引用類型的值,不允許傳遞基礎(chǔ)類型的值。
const reactive1 = reactive(0) // false const reactive2 = reactive({ count: 0 }) // true
reactive
用于創(chuàng)建一個響應(yīng)式對象,可以包含多個屬性。通過reactive,我們可以將整個對象變成響應(yīng)式,使對象的任何屬性發(fā)生變化時都能被檢測到。
訪問數(shù)據(jù)的方式不同
對于 ref
來說,無論是原始類型還是對象,訪問數(shù)據(jù)都需要通過 .value
的形式進(jìn)行,更新數(shù)據(jù)也需要通過 .value
來更新。
<template> <div>{{ dxc }}</div> </template> <script setup> import { ref } from 'vue' const dxc = ref(0) </script>
但在<template>
中使用ref的值時不需要帶上.value
。
const ref1 = ref(0) console.log(ref1.value) // 0 const ref2 = ref({ count: 0 }) console.log(ref2.value.count) // 0 ref1.value = 1 console.log(ref1.value) // 1
watch監(jiān)聽方式不同
ref
可以直接監(jiān)聽數(shù)據(jù),當(dāng)數(shù)據(jù)發(fā)生變化的時候,就會執(zhí)行watch
函數(shù)對應(yīng)的回調(diào)。
const ref1 = ref(0) watch(ref1, () => { console.log('changed!') })
這里只是原始類型數(shù)據(jù),如果是對象的話,需要深度監(jiān)聽deep:true
。
const ref1 = ref({num: 1}) watch(ref1, () => { console.log('changed!') }) // ref1.value.num = 1 // 執(zhí)行該語句時并不會觸發(fā)watch監(jiān)聽,watch 并沒有對ref1進(jìn)行深度監(jiān)聽 // 但注意,此時dom是能更新的,ref會將其轉(zhuǎn)換成 reactive 的形式 // 要想深入監(jiān)聽,只需要加一個對應(yīng)的參數(shù)即可 const ref1 = ref({num: 1}) watch(ref1, () => { console.log('changed!') }, { deep: true })
reactive
因為本質(zhì)是對象,所以在 watch 的時候本能的會添加deep
屬性。vue 對其做了優(yōu)化watch
監(jiān)聽 reactive 的時候可以不添加deep
屬也能夠?qū)ζ渥錾疃缺O(jiān)聽。
const reactive1 = reactive({num: 1}) watch(reactive1, () => { console.log('changed!') }) // reactive1.num = 1 // 觸發(fā)watch監(jiān)聽
Ref基本用法及在setup()中的使用
基本用法
在Vue3
中,ref
用于創(chuàng)建一個響應(yīng)式的基本數(shù)據(jù)類型,如數(shù)字、字符串等。通過ref()
函數(shù)創(chuàng)建一個ref
對象,然后可以通過.value
來訪問和修改數(shù)據(jù)值。
import { ref } from 'vue'; const count = ref(0); // 創(chuàng)建一個Ref對象,初始值為0 console.log(count.value); // 訪問Ref對象的值 count.value = 1; // 修改Ref對象的值
在setup()中使用
在setup()
函數(shù)中,我們可以使用ref()
來創(chuàng)建響應(yīng)式數(shù)據(jù),以便在組件中使用。
import { ref } from 'vue'; export default { setup() { const count = ref(0); return { count }; } };
<script setup>
語法是一種簡潔的寫法,可以在單文件組件中更便捷地使用ref
。
<script setup> import { ref } from 'vue'; const count = ref(0); </script>
Reactive基本概念及在模板中的使用
基本概念
在Vue3
中,reactive
用于創(chuàng)建一個響應(yīng)式對象,使對象的屬性發(fā)生變化時能夠被檢測到。通過reactive()
函數(shù)創(chuàng)建一個響應(yīng)式對象,對象的所有屬性都變成響應(yīng)式。
import { reactive } from 'vue'; const user = reactive({ name: 'Alice', age: 30 });
在模板中使用Reactive
在模板中可以直接使用響應(yīng)式對象,對對象的屬性進(jìn)行訪問和修改。
<template> <div> <p>Name: {{ user.name }}</p> <p>Age: {{ user.age }}</p> </div> </template> <script> import { reactive } from 'vue'; export default { setup() { const user = reactive({ name: 'Alice', age: 30 }); return { user }; } }; </script>
ref與reactive用法總結(jié)
ref
- 基本用法:
Ref
用于創(chuàng)建一個響應(yīng)式的基本數(shù)據(jù)類型,如數(shù)字、字符串等。通過ref
函數(shù)創(chuàng)建,訪問和修改數(shù)據(jù)值需要使用.value
。- 在setup()中使用:在
setup()
函數(shù)中,我們可以使用ref
來創(chuàng)建響應(yīng)式數(shù)據(jù),并在模板中使用。- <script setup>語法:
<script setup>
語法是Vue3推薦的一種寫法,可以在單文件組件中更簡潔地使用ref。- 為何使用ref:
Ref
適用于管理簡單的基本數(shù)據(jù)類型,如數(shù)字、字符串等。
reactive
- 基本概念:
Reactive
用于創(chuàng)建一個響應(yīng)式對象,可以包含多個屬性。通過reactive
函數(shù)創(chuàng)建,對象的任何屬性變化都會被檢測到。- 在模板中使用
Reactive
:在模板中可以直接使用響應(yīng)式對象,對對象的屬性進(jìn)行訪問和修改。- 深層響應(yīng)式:
Reactive
會遞歸地將對象的所有嵌套屬性都變成響應(yīng)式。- 與ref區(qū)別:
Ref
適用于簡單數(shù)據(jù)類型,而Reactive
適用于對象,可以處理對象的多個屬性。- 為何使用
Reactive
:Reactive
適用于管理復(fù)雜對象,使整個對象都變成響應(yīng)式。
附:ref和reactive定義數(shù)組對比
使用ref定義數(shù)組舉例如下
const tableData = ref([]) // 定義 const getTableData = async () => { const { data } = await getTableDataApi() // 模擬接口獲取表格數(shù)據(jù) tableData.value = data // 修改 }
<!-- Vue3模板引用使用 --> <a-table v-model:dataSource="tableData"></a-table>
圖中以我們常用的表格數(shù)據(jù)舉例,可以看到,ref定義數(shù)組與定義基本數(shù)據(jù)類型沒什么差別,接下來看看reactive
const tableData = reactive([]) // 定義 const getTableData = async () => { const { data } = await getTableDataApi() // 模擬接口獲取表格數(shù)據(jù) tableData = data // 修改,錯誤示例,這樣賦值會使tableData失去響應(yīng)式 }
<!-- Vue3模板引用使用 --> <a-table v-model:dataSource="tableData"></a-table>
需要注意的是,使用 tableData = data 的修改方式會造成 tableData 響應(yīng)式丟失,解決方法如下(供參考)
// 方法一:改為 ref 定義 const tableData = ref([]) const getTableData = async () => { const { data } = await getTableDataApi() tableData.value = data // 使用.value重新賦值 } // 方法二:使用 push 方法 const tableData = reactive([]) const getTableData = async () => { const { data } = await getTableDataApi() tableData.push(...data) // 先使用...將data解構(gòu),再使用push方法 } // 方法三:定義時數(shù)組外層嵌套一個對象 const tableData = reactive({ list:[] }) const getTableData = async () => { const { data } = await getTableDataApi() tableData.list = data // 通過訪問list屬性重新賦值 } // 方法四:賦值前再包一層 reactive const tableData = reactive([]) const getTableData = async () => { const { data } = await getTableDataApi() tableData = reactive(data) // 賦值前再包一層reactive }
總結(jié)
到此這篇關(guān)于前端vue3中的ref與reactive用法及區(qū)別總結(jié)的文章就介紹到這了,更多相關(guān)vue3中ref與reactive內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Vue3.0中Ref與Reactive的區(qū)別示例詳析
- vue3?中ref和reactive的區(qū)別講解
- Vue3 的ref和reactive的用法和區(qū)別示例解析
- Vue3中ref和reactive的基本使用及區(qū)別詳析
- Vue3中ref和reactive的區(qū)別及說明
- vue3.0中ref與reactive的區(qū)別及使用場景分析
- Vue3中關(guān)于ref和reactive的區(qū)別分析
- vue3中reactive和ref的實現(xiàn)與區(qū)別詳解
- vue3 ref 和reactive的區(qū)別詳解
- vue3中ref和reactive的區(qū)別舉例詳解
相關(guān)文章
vue 項目中的this.$get,this.$post等$的用法案例詳解
vue.js的插件應(yīng)該暴露一個install方法。這個方法的第一個參數(shù)是vue構(gòu)造器,第二個參數(shù)是一個可選的選項對象,首頁要安裝axios,本文結(jié)合案例代碼給大家詳細(xì)講解vue 中的this.$get,this.$post等$的用法,一起學(xué)習(xí)下吧2022-12-12Vue3如何通過ESLint校驗代碼是否符合規(guī)范詳解
ESLint可以靈活設(shè)置規(guī)則,也發(fā)布了很多公開的規(guī)則集,下面這篇文章主要給大家介紹了關(guān)于Vue3如何通過ESLint校驗代碼是否符合規(guī)范的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07詳解vue-cli 快速搭建單頁應(yīng)用之遇到的問題及解決辦法
這篇文章主要介紹了詳解vue-cli 快速搭建單頁應(yīng)用之遇到的問題及解決辦法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03