vue3+ts深入組件Props實(shí)例詳解
在Vue 3和TypeScript中,深入了解組件的Props是非常重要的。Props是組件之間進(jìn)行數(shù)據(jù)傳遞的一種方式,可以將數(shù)據(jù)從父組件傳遞給子組件。
首先,在Vue 3中定義Props的方式有所改變。在組件的選項(xiàng)中,我們可以使用props
屬性來(lái)定義Props的類型和驗(yàn)證規(guī)則。例如:
import { defineComponent, PropType } from 'vue'; export default defineComponent({ props: { // 基本類型的Props name: { type: String, required: true }, age: { type: Number, default: 18 }, // 自定義類型的Props person: { type: Object as PropType<{ name: string, age: number }>, required: true }, // 數(shù)組類型的Props hobbies: { type: Array as PropType<string[]>, default: () => [] } }, // ... });
在上面的例子中,我們定義了幾個(gè)不同類型的Props。name
是一個(gè)必需的字符串類型的Props,age
是一個(gè)可選的數(shù)字類型的Props,默認(rèn)值為18。person
是一個(gè)必需的自定義類型的Props,它是一個(gè)包含name
和age
屬性的對(duì)象。hobbies
是一個(gè)可選的數(shù)組類型的Props,默認(rèn)值為空數(shù)組。
在使用Props時(shí),我們可以在子組件中通過(guò)props
選項(xiàng)來(lái)訪問(wèn)它們。例如:
import { defineComponent } from 'vue'; export default defineComponent({ props: ['name', 'age', 'person', 'hobbies'], // ... created() { console.log(this.name); // 訪問(wèn)字符串類型的Props console.log(this.age); // 訪問(wèn)數(shù)字類型的Props console.log(this.person); // 訪問(wèn)自定義類型的Props console.log(this.hobbies); // 訪問(wèn)數(shù)組類型的Props } });
在上面的例子中,我們通過(guò)props
選項(xiàng)將Props聲明為組件的屬性。然后,在組件的created
生命周期鉤子中,我們可以通過(guò)this
關(guān)鍵字來(lái)訪問(wèn)這些Props。
此外,還可以使用TypeScript的類型注解來(lái)提供Props的類型檢查。例如:
import { defineComponent } from 'vue'; interface Person { name: string; age: number; } export default defineComponent({ props: { person: { type: Object as () => Person, required: true } }, // ... });
在上面的例子中,我們使用了TypeScript的接口來(lái)定義Person
類型,并在props
選項(xiàng)中使用了類型注解來(lái)指定person
的類型為Person
。
總結(jié)一下,在Vue 3和TypeScript中,我們可以使用props
選項(xiàng)來(lái)定義和驗(yàn)證組件的Props??梢允褂貌煌愋偷腜rops,包括基本類型、自定義類型和數(shù)組類型。在子組件中,可以通過(guò)props
選項(xiàng)來(lái)訪問(wèn)這些Props,并使用TypeScript的類型注解來(lái)提供類型檢查。這樣可以更安全和可靠地進(jìn)行組件間的數(shù)據(jù)傳遞。
傳遞靜態(tài)prop
在Vue 3和TypeScript中,如果要傳遞靜態(tài)的Props,可以在父組件中直接在子組件的標(biāo)簽上使用Props的語(yǔ)法來(lái)傳遞靜態(tài)值。
例如,假設(shè)我們有一個(gè)子組件ChildComponent
,它有一個(gè)接受字符串類型的Props message
。
import { defineComponent, PropType } from 'vue'; export default defineComponent({ props: { message: { type: String, required: true } }, // ... });
在父組件中,可以在子組件的標(biāo)簽上使用Props的語(yǔ)法來(lái)傳遞靜態(tài)值。
<template> <div> <child-component message="Hello, World!"></child-component> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent } }; </script>
在上面的例子中,我們?cè)?code>ChildComponent的標(biāo)簽上使用message
屬性,并傳遞了靜態(tài)的字符串值"Hello, World!"。
在子組件中,可以通過(guò)props
選項(xiàng)來(lái)接收傳遞的靜態(tài)Props。
import { defineComponent } from 'vue'; export default defineComponent({ props: ['message'], // ... created() { console.log(this.message); // 輸出:Hello, World! } });
在上面的例子中,我們?cè)谧咏M件的created
生命周期鉤子中,通過(guò)this.message
來(lái)訪問(wèn)傳遞的靜態(tài)Props。
傳遞非字符串類型,使用v-bind
如果要傳遞非字符串類型的Props,并且希望使用動(dòng)態(tài)的值,可以使用v-bind
指令來(lái)綁定Props。
例如,假設(shè)我們有一個(gè)子組件ChildComponent
,它有一個(gè)接受數(shù)字類型的Props count
。
import { defineComponent, PropType } from 'vue'; export default defineComponent({ props: { count: { type: Number, required: true } }, // ... });
在父組件中,可以使用v-bind
指令來(lái)綁定Props的值。
<template> <div> <child-component :count="totalCount"></child-component> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, data() { return { totalCount: 10 }; } }; </script>
在上面的例子中,我們使用v-bind
指令來(lái)綁定子組件的count
屬性,并將其值綁定到父組件的totalCount
變量上。
在子組件中,可以通過(guò)props
選項(xiàng)來(lái)接收傳遞的動(dòng)態(tài)Props。
import { defineComponent } from 'vue'; export default defineComponent({ props: ['count'], // ... created() { console.log(this.count); // 輸出:10 } });
在上面的例子中,我們?cè)谧咏M件的created
生命周期鉤子中,通過(guò)this.count
來(lái)訪問(wèn)傳遞的動(dòng)態(tài)Props。
prop校驗(yàn),單向數(shù)據(jù)流
在Vue中,可以通過(guò)使用props
選項(xiàng)來(lái)對(duì)Props進(jìn)行校驗(yàn),以確保傳遞給組件的數(shù)據(jù)滿足特定的要求。
例如,假設(shè)我們有一個(gè)子組件ChildComponent
,它有一個(gè)接受數(shù)字類型的Props count
,并且要求傳遞的值必須大于0。
import { defineComponent, PropType } from 'vue'; export default defineComponent({ props: { count: { type: Number, required: true, validator: (value: number) => value > 0 } }, // ... });
在上面的例子中,我們?cè)?code>props選項(xiàng)中定義了count
屬性,并指定了它的類型為Number
,并且設(shè)置了required: true
,表示這個(gè)Props是必需的。同時(shí),我們還使用了一個(gè)自定義的校驗(yàn)函數(shù)validator
,該函數(shù)接受傳遞的值作為參數(shù),返回一個(gè)布爾值,用于校驗(yàn)傳遞的值是否滿足要求。
在父組件中,如果傳遞的Props不滿足校驗(yàn)要求,Vue會(huì)在控制臺(tái)中輸出警告信息。
<template> <div> <child-component :count="totalCount"></child-component> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, data() { return { totalCount: -5 }; } }; </script>
在上面的例子中,我們?cè)诟附M件中將totalCount
設(shè)置為-5,這違反了子組件的校驗(yàn)規(guī)則。
到此這篇關(guān)于vue3+ts深入組件Props實(shí)例詳解的文章就介紹到這了,更多相關(guān)vue3+ts深入組件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue利用vue-baidu-map實(shí)現(xiàn)獲取經(jīng)緯度和搜索地址
在開發(fā)項(xiàng)目的時(shí)候,發(fā)現(xiàn)需要獲取經(jīng)緯度,由于這個(gè)項(xiàng)目是用vue寫的,最后決定使用vue-baidu-map來(lái)快速獲取經(jīng)緯度,感興趣的可以了解一下2022-09-09VUE?Element修改el-input和el-select長(zhǎng)度的具體步驟
這篇文章主要給大家介紹了關(guān)于VUE?Element修改el-input和el-select長(zhǎng)度的具體步驟,文中通過(guò)代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-12-12Vue項(xiàng)目如何保持用戶登錄狀態(tài)(localStorage+vuex刷新頁(yè)面后狀態(tài)依然保持)
關(guān)于vue登錄注冊(cè),并保持登錄狀態(tài),是vue玩家必經(jīng)之路,這篇文章主要給大家介紹了關(guān)于Vue項(xiàng)目如何保持用戶登錄狀態(tài)的相關(guān)資料,localStorage+vuex刷新頁(yè)面后狀態(tài)依然保持,需要的朋友可以參考下2022-05-05從Vuex中取出數(shù)組賦值給新的數(shù)組,新數(shù)組push時(shí)報(bào)錯(cuò)的解決方法
今天小編就為大家分享一篇從Vuex中取出數(shù)組賦值給新的數(shù)組,新數(shù)組push時(shí)報(bào)錯(cuò)的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09Vue.js中實(shí)現(xiàn)密碼修改及頁(yè)面跳轉(zhuǎn)和刷新的完整指南
在現(xiàn)代Web應(yīng)用中,用戶賬戶管理是一個(gè)核心功能,其中密碼修改是一個(gè)常見(jiàn)的需求,本文將詳細(xì)介紹如何在Vue.js應(yīng)用中實(shí)現(xiàn)用戶密碼修改功能,并在成功后跳轉(zhuǎn)到登錄頁(yè)面并刷新該頁(yè)面,需要的朋友可以參考下2024-12-12