日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > vue >内容正文

vue

Vue 3.0响应式API案例

發(fā)布時(shí)間:2024/1/23 vue 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Vue 3.0响应式API案例 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

什么是Proxy
proxy翻譯過來的意思就是”代理“,ES6對Proxy的定位就是target對象(原對象)的基礎(chǔ)上通過handler增加一層”攔截“,返回一個新的代理對象,之后所有在Proxy中被攔截的屬性,都可以定制化一些新的流程在上面,先看一個最簡單的例子。

<!DOCTYPE html> <html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>reactive方法和watchEffect方法</title><script src="https://unpkg.com/vue@next"></script> </head><body><div id="app"><post-item :post-content="content"></post-item></div><script>//創(chuàng)建組件;const { reactive, watchEffect } = Vue;//創(chuàng)建常量;const state = reactive({ count: 0 });//函數(shù)方法;watchEffect(() => {document.body.innerHTML = `商品庫存為:${state.count}`});</script> </body></html>

其實(shí)ref除了可以獲取本頁面的dom元素,還可以拿到子組件中的data和去調(diào)用子組件中的方法

獲取子組件中的data
子組件

<!DOCTYPE html> <html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>ref方法</title><script src="https://unpkg.com/vue@next"></script> </head><body><div id="app"><post-item :post-content="content"></post-item></div><script>const { ref, watchEffect } = Vue;//創(chuàng)建函數(shù)const state = ref(0)watchEffect(() => {document.body.innerHTML = `商品庫存為:${state.value}`});</script> </body></html>

vue3 中 的 computed 的使用,由于 vue3 兼容 vue2 的選項(xiàng)式API,所以可以直接使用 vue2的寫法,這篇文章主要介紹 vue3 中 computed 的新用法,對比 vue2 中的寫法,讓您快速掌握 vue3 中 computed 的新用法。

組合式 API 中使用 computed 時(shí),需要先引入:import { computed } from “vue”。引入之后 computed 可以傳入的參數(shù)有兩種:回調(diào)函數(shù)和 options 。具體看看它是如何使用的?

一、函數(shù)式寫法
在vue2中,computed 寫法:

computed:{ sum(){ return this.num1+ this.num2 } }
在vue3 如果使用選項(xiàng)式API也可以這樣寫,主要看下組合式API的使用。

示例1:求和

import { ref, computed } from “vue” export default{ setup(){ const num1 = ref(1) const num2 = ref(1) let sum = computed(()=>{ return num1.value + num2.value }) } }
調(diào)用 computed 時(shí), 傳入了一個箭頭函數(shù),返回值作為 sum 。相比之前,使用更加簡單了。如果需要計(jì)算多個屬性值,直接調(diào)用就可以。如:

let sum = computed(()=>{ return num1.value + num2.value }) let mul = computed(()=>{ return num1.value * num2.value })
該示例過于簡單,看不明白的可在文章后面閱讀完整實(shí)例1。

二、options 寫法
計(jì)算屬性默認(rèn)只有 getter ,在需要的時(shí)候也可以提供 setter 。在vue2中用法如下:

computed:{ mul:{ get(){ // num1值改變時(shí)觸發(fā) return this.num1 * 10 }, set(value){ // mul值被改變時(shí)觸發(fā) this.num1 = value /10 } } }
mul 屬性是 給num1 放大10,如果修改 mul 的值,則 num1 也隨之改變。

在 vue3 中 :

let mul = computed({ get:()=>{ return num1.value *10 }, set:(value)=>{ return num1.value = value/10 } })
這兩種寫法不太一樣,仔細(xì)觀察區(qū)別不大,get 和 set 調(diào)用也是一樣的。

<!DOCTYPE html> <html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>computed方法</title><script src="https://unpkg.com/vue@next"></script> </head><body><div id="app"><p>原始字符串:{{msg}}</p><p>反轉(zhuǎn)后的字符串:{{revmsg}}</p></div>3VrzcxJNG YYYYFUO[[PONPOPIWDAEWZSW55z[,[,]]]]<script>const { ref, computed } = Vue;const vm = Vue.createApp({setup() {const msg = ref('人世幾回傷往事');const revmsg = computed(() =>msg.value.split('').reverse().join(''));return {msg,revmsg}}});vm.mount('#app');</script> </body></html>

Vue3.0使用組件創(chuàng)建樹形項(xiàng)目分類綜合案例

<!DOCTYPE html> <html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>使用組件創(chuàng)建樹狀項(xiàng)目</title><script src="https://unpkg.com/vue@next"></script> </head><body><div id="app"><category-component :list="categories"></category-component></div><script>const CategoryComponent = {name: 'catComp',props: {list: { type: Array }},template: `<ul><template v-if="list"><li v-for="cat in list">{{cat.name}}<catComp :list="cat.children"/></li></template></ul>`}//繼續(xù)const app = Vue.createApp({data() {return {categories: [{name: 'JAVA編程講義',children: [{name: 'JAVA編程思想',children: [{ name: 'HTML開發(fā)技術(shù)' },{ name: 'JS開發(fā)技術(shù)' },{ name: 'Vue.js開發(fā)技術(shù)' }]}, {name: 'C#開發(fā)指南'}]},{name: 'JAVA編程講義',children: [{name: 'JAVA編程思想',children: [{ name: 'C#宿舍管理系統(tǒng)實(shí)戰(zhàn)' },{ name: 'Android開發(fā)技術(shù)' },{ name: '鴻蒙開發(fā)技術(shù)' }]}, {name: '張晨光老師帶你學(xué)Vue'}]}]}},components: {CategoryComponent}}).mount('#app');</script> </body></html>

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎

總結(jié)

以上是生活随笔為你收集整理的Vue 3.0响应式API案例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。