Vue3中props傳參方式詳解
在Vue3中,`props`接收的`type`類型有以下幾種:
1. String:字符串類型
2. Number:數(shù)字類型
3. Boolean:布爾類型
4. Array:數(shù)組類型
5. Object:對(duì)象類型
6. Date:日期類型
7. Function:函數(shù)類型
8. Symbol:符號(hào)類型
9. [Custom Types]:自定義類型
你也可以使用數(shù)組形式來表示多種類型的組合,
比如`[String, Number]`表示接收字符串或數(shù)字類型的值。
另外,你還可以使用`null`或`undefined`來表示接收任意類型的值。
注意:以上是常見的`type`類型列表,你也可以自定義其它類型。
`props` 還有兩個(gè)參數(shù):
default: 默認(rèn)值
required: 是否必傳, true必傳,false 非必傳
開啟必傳時(shí) 若不傳則警告[Vue warn]: Missing required prop: "xxx"
父組件代碼(測試默認(rèn)值)
<template> <div style="font-size: 14px"> <h3>測試props傳參常見的數(shù)據(jù)類型</h3> <Child :message="message" /> <!-- :count="count" :isActive="isActive" :list="list" :user="user" :date="date" :callback="callback --> </div> </template> <script lang="ts"> import { defineComponent, reactive, onMounted, toRefs } from 'vue' import Child from './Child.vue' // vue3.0語法 export default defineComponent({ name: '父組件名', components: { Child, }, setup() { // 在Vue3中,`props`接收的`type`類型有以下幾種: // 1. String:字符串類型 // 2. Number:數(shù)字類型 // 3. Boolean:布爾類型 // 4. Array:數(shù)組類型 // 5. Object:對(duì)象類型 // 6. Date:日期類型 // 7. Function:函數(shù)類型 // 8. Symbol:符號(hào)類型 // 9. [Custom Types]:自定義類型 // 你也可以使用數(shù)組形式來表示多種類型的組合, // 比如`[String, Number]`表示接收字符串或數(shù)字類型的值。 // 另外,你還可以使用`null`或`undefined`來表示接收任意類型的值。 // 注意:以上是常見的`type`類型列表,你也可以自定義其它類型。 const state = reactive({ date: new Date(1998, 12, 31), message: 'Hello World', count: 666, isActive: true, list: [1, 2, 3], user: { name: '張三', age: 18, }, callback: () => { console.log('父組件傳入的callback執(zhí)行了') }, }) onMounted(() => { // }) return { ...toRefs(state), } }, }) </script>
子組件代碼
<template> <div style="font-size: 14px"> <!-- 子組件內(nèi)容 --> <p>message: {{ message }}</p> <p>count: {{ count }}</p> <p>isActive: {{ isActive }}</p> <p>list: {{ list }}</p> <p v-for="(item,index) in list" :key="index">{{ item }}</p> <p>date: {{ date }}</p> <p>user: {{ user }}</p> <button @click="callback">callback按鈕(調(diào)用父組件函數(shù))</button> </div> </template> <script lang="ts"> import { defineComponent, onMounted } from 'vue' // vue3.0語法 export default defineComponent({ name: '子組件名', props: { message: { type: String, // type 類型 required: true, // required 是否必傳, true必傳 若不傳則警告[Vue warn]: Missing required prop: "message" }, count: { type: Number, default: 0, // default 默認(rèn)值 }, isActive: { type: Boolean, default: false, }, list: { type: Array, default: () => [], }, date: { type: Date, default: () => new Date(), }, user: { type: Object, default: () => ({ name: 'John Doe', email: 'johndoe@mail.com' }), }, callback: { type: Function, default: () => {}, }, }, setup(props) { onMounted(() => { console.log('props', props) }) return { // } }, }) </script>
頁面數(shù)據(jù)顯示效果(只傳了必填項(xiàng)message)
可以看到,接收到的props只有message是父組件傳來的值,而子組件顯示的其它值都是定義在default里的默認(rèn)值,點(diǎn)擊callback按鈕(調(diào)用父組件函數(shù))也是沒有任何反應(yīng)的。
修改父組件代碼(將各種數(shù)據(jù)類型傳入)
<template> <div style="font-size: 14px"> <h3>測試props傳參常見的數(shù)據(jù)類型</h3> <Child :message="message" :count="count" :is-active="isActive" :list="list" :user="user" :date="date" :callback="callback" /> <!-- 兩種命名方式都可以 :is-active="isActive" :isActive="isActive" --> </div> </template> <script lang="ts"> import { defineComponent, reactive, onMounted, toRefs } from 'vue' import Child from './Child.vue' // vue3.0語法 export default defineComponent({ name: '父組件名', components: { Child, }, setup() { // 在Vue3中,`props`接收的`type`類型有以下幾種: // 1. String:字符串類型 // 2. Number:數(shù)字類型 // 3. Boolean:布爾類型 // 4. Array:數(shù)組類型 // 5. Object:對(duì)象類型 // 6. Date:日期類型 // 7. Function:函數(shù)類型 // 8. Symbol:符號(hào)類型 // 9. [Custom Types]:自定義類型 // 你也可以使用數(shù)組形式來表示多種類型的組合, // 比如`[String, Number]`表示接收字符串或數(shù)字類型的值。 // 另外,你還可以使用`null`或`undefined`來表示接收任意類型的值。 // 注意:以上是常見的`type`類型列表,你也可以自定義其它類型。 const state = reactive({ date: new Date(1998, 12, 31), message: 'Hello World', count: 666, isActive: true, list: [1, 2, 3], user: { name: '張三', age: 18, }, callback: () => { console.log('父組件傳入的callback執(zhí)行了') }, }) onMounted(() => { // }) return { ...toRefs(state), } }, }) </script>
頁面數(shù)據(jù)顯示效果(各種數(shù)據(jù)類型傳入了)
可以看到數(shù)據(jù)將以父組件傳入的值為準(zhǔn),default的值被覆蓋。點(diǎn)擊callback按鈕(調(diào)用父組件函數(shù))也執(zhí)行了。
踩坑小案例
案例:父組件的數(shù)據(jù)是從接口異步請求來的 ,而子組件是會(huì)先掛載的,如果子組件接受的值是從父組件的接口里取來的,想在子組件onMounted的時(shí)候拿到這個(gè)數(shù)據(jù)來發(fā)請求卻沒拿到。
修改代碼(看下案例):
父組件代碼
<template> <div style="font-size: 14px"> <h3>測試props傳參常見的數(shù)據(jù)類型</h3> <Child :id="id" :message="message" :count="count" :is-active="isActive" :list="list" :user="user" :date="date" :callback="callback" /> <!-- 兩種命名方式都可以 :is-active="isActive" :isActive="isActive" --> </div> </template> <script lang="ts"> import { defineComponent, reactive, onMounted, toRefs } from 'vue' import Child from './Child.vue' // vue3.0語法 export default defineComponent({ name: '父組件名', components: { Child, }, setup() { // 在Vue3中,`props`接收的`type`類型有以下幾種: // 1. String:字符串類型 // 2. Number:數(shù)字類型 // 3. Boolean:布爾類型 // 4. Array:數(shù)組類型 // 5. Object:對(duì)象類型 // 6. Date:日期類型 // 7. Function:函數(shù)類型 // 8. Symbol:符號(hào)類型 // 9. [Custom Types]:自定義類型 // 你也可以使用數(shù)組形式來表示多種類型的組合, // 比如`[String, Number]`表示接收字符串或數(shù)字類型的值。 // 另外,你還可以使用`null`或`undefined`來表示接收任意類型的值。 // 注意:以上是常見的`type`類型列表,你也可以自定義其它類型。 const state = reactive({ id: '', date: new Date(1998, 12, 31), message: '', count: 666, isActive: true, list: [1, 2, 3], user: { name: '張三', age: 18, }, callback: () => { console.log('父組件傳入的callback執(zhí)行了') }, }) onMounted(() => { // 模擬一個(gè)接口請求 setTimeout(() => { state.id = '父組件請求接口得來的id' }, 3000) }) return { ...toRefs(state), } }, }) </script>
子組件代碼:
<template> <div style="font-size: 14px"> <!-- 子組件內(nèi)容 --> <p>message: {{ message }}</p> <p>count: {{ count }}</p> <p>isActive: {{ isActive }}</p> <p>list: {{ list }}</p> <p v-for="(item,index) in list" :key="index">{{ item }}</p> <p>date: {{ date }}</p> <p>user: {{ user }}</p> <button @click="callback">callback按鈕(調(diào)用父組件函數(shù))</button> </div> </template> <script lang="ts"> import { defineComponent, onMounted } from 'vue' // vue3.0語法 export default defineComponent({ name: '子組件名', props: { id: { type: String, // type 類型 required: true, // required 是否必傳, true必傳 若不傳則警告[Vue warn]: Missing required prop: "message" }, message: { type: String, // type 類型 required: true, // required 是否必傳, true必傳 若不傳則警告[Vue warn]: Missing required prop: "message" }, count: { type: Number, default: 0, // default 默認(rèn)值 }, isActive: { type: Boolean, default: false, }, list: { type: Array, default: () => [], }, date: { type: Date, default: () => new Date(), }, user: { type: Object, default: () => ({ name: 'John Doe', email: 'johndoe@mail.com' }), }, callback: { type: Function, default: () => {}, }, }, setup(props) { onMounted(() => { console.log('props', props) console.log('props.id:', props.id) // 想拿到id后請求接口 // axios.get('props.id').then(res => { // console.log(res) // }) }) return { // } }, }) </script>
案例顯示效果(取不到id)
父組件請求接口的數(shù)據(jù)最終會(huì)在子組件更新,但是想在onMounted里使用卻是拿不到的,因?yàn)榻涌谶€沒請求完成,沒拿到該數(shù)據(jù),我們來嘗試解決這個(gè)問題。
解決方案1(v-if)
修改父組件代碼:
<template> <div style="font-size: 14px"> <h3>測試props傳參常見的數(shù)據(jù)類型</h3> <Child v-if="id" :id="id" :message="message" :count="count" :is-active="isActive" :list="list" :user="user" :date="date" :callback="callback" /> <!-- 兩種命名方式都可以 :is-active="isActive" :isActive="isActive" --> </div> </template> <script lang="ts"> import { defineComponent, reactive, onMounted, toRefs } from 'vue' import Child from './Child.vue' // vue3.0語法 export default defineComponent({ name: '父組件名', components: { Child, }, setup() { // 在Vue3中,`props`接收的`type`類型有以下幾種: // 1. String:字符串類型 // 2. Number:數(shù)字類型 // 3. Boolean:布爾類型 // 4. Array:數(shù)組類型 // 5. Object:對(duì)象類型 // 6. Date:日期類型 // 7. Function:函數(shù)類型 // 8. Symbol:符號(hào)類型 // 9. [Custom Types]:自定義類型 // 你也可以使用數(shù)組形式來表示多種類型的組合, // 比如`[String, Number]`表示接收字符串或數(shù)字類型的值。 // 另外,你還可以使用`null`或`undefined`來表示接收任意類型的值。 // 注意:以上是常見的`type`類型列表,你也可以自定義其它類型。 const state = reactive({ id: '', date: new Date(1998, 12, 31), message: '', count: 666, isActive: true, list: [1, 2, 3], user: { name: '張三', age: 18, }, callback: () => { console.log('父組件傳入的callback執(zhí)行了') }, }) onMounted(() => { // 模擬一個(gè)接口請求 setTimeout(() => { state.id = '父組件請求接口得來的id' }, 3000) }) return { ...toRefs(state), } }, }) </script>
解決方案1(v-if)頁面效果
在使用子組件的標(biāo)簽上加上<Child v-if="id"/>,沒有拿到id時(shí)子組件并不會(huì)渲染,當(dāng)然接口如果過慢的話子組件也會(huì)渲染更慢。
解決方案2(父組件不加v-if,子組件用watchEffect)
父組件代碼:
<template> <div style="font-size: 14px"> <h3>測試props傳參常見的數(shù)據(jù)類型</h3> <Child :id="id" :message="message" :count="count" :is-active="isActive" :list="list" :user="user" :date="date" :callback="callback" /> <!-- 兩種命名方式都可以 :is-active="isActive" :isActive="isActive" --> </div> </template> <script lang="ts"> import { defineComponent, reactive, onMounted, toRefs } from 'vue' import Child from './Child.vue' // vue3.0語法 export default defineComponent({ name: '父組件名', components: { Child, }, setup() { // 在Vue3中,`props`接收的`type`類型有以下幾種: // 1. String:字符串類型 // 2. Number:數(shù)字類型 // 3. Boolean:布爾類型 // 4. Array:數(shù)組類型 // 5. Object:對(duì)象類型 // 6. Date:日期類型 // 7. Function:函數(shù)類型 // 8. Symbol:符號(hào)類型 // 9. [Custom Types]:自定義類型 // 你也可以使用數(shù)組形式來表示多種類型的組合, // 比如`[String, Number]`表示接收字符串或數(shù)字類型的值。 // 另外,你還可以使用`null`或`undefined`來表示接收任意類型的值。 // 注意:以上是常見的`type`類型列表,你也可以自定義其它類型。 const state = reactive({ id: '', date: new Date(1998, 12, 31), message: '', count: 666, isActive: true, list: [1, 2, 3], user: { name: '張三', age: 18, }, callback: () => { console.log('父組件傳入的callback執(zhí)行了') }, }) onMounted(() => { // 模擬一個(gè)接口請求 setTimeout(() => { state.id = '父組件請求接口得來的id' }, 3000) }) return { ...toRefs(state), } }, }) </script>
子組件代碼
<template> <div style="font-size: 14px"> <!-- 子組件內(nèi)容 --> <p>message: {{ message }}</p> <p>count: {{ count }}</p> <p>isActive: {{ isActive }}</p> <p>list: {{ list }}</p> <p v-for="(item,index) in list" :key="index">{{ item }}</p> <p>date: {{ date }}</p> <p>user: {{ user }}</p> <button @click="callback">callback按鈕(調(diào)用父組件函數(shù))</button> </div> </template> <script lang="ts"> import { defineComponent, onMounted, watchEffect } from 'vue' // vue3.0語法 export default defineComponent({ name: '子組件名', props: { id: { type: String, // type 類型 required: true, // required 是否必傳, true必傳 若不傳則警告[Vue warn]: Missing required prop: "message" }, message: { type: String, // type 類型 required: true, // required 是否必傳, true必傳 若不傳則警告[Vue warn]: Missing required prop: "message" }, count: { type: Number, default: 0, // default 默認(rèn)值 }, isActive: { type: Boolean, default: false, }, list: { type: Array, default: () => [], }, date: { type: Date, default: () => new Date(), }, user: { type: Object, default: () => ({ name: 'John Doe', email: 'johndoe@mail.com' }), }, callback: { type: Function, default: () => {}, }, }, setup(props) { onMounted(() => { console.log('onMounted props', props) console.log('onMounted props.id:', props.id) // 想拿到id后請求接口 // axios.get('props.id').then(res => { // console.log(res) // }) }) watchEffect(() => { console.log('watchEffect', props.id) if (props.id) { // 想拿到id后請求接口 // axios.get('props.id').then(res => { // console.log(res) // }) } }) return { // } }, }) </script>
解決方案2watchEffect的頁面顯示效果
可以看到子組件的頁面依然會(huì)先掛載渲染,onMounted雖然拿不到值,但是可以在watchEffect里檢測到id有值了再做請求就行了。當(dāng)然有其它的解決方案也歡迎評(píng)論區(qū)留言交流。
以上就是Vue3中props傳參方式詳解的詳細(xì)內(nèi)容,更多關(guān)于Vue3 props的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue3+ts數(shù)組去重方及reactive/ref響應(yīng)式顯示流程分析
這篇文章主要介紹了vue3+ts數(shù)組去重方法-reactive/ref響應(yīng)式顯示,本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04ElementUI中的el-dropdown傳入多參數(shù)的實(shí)現(xiàn)方法
本文主要介紹了ElementUI中的el-dropdown傳入多參數(shù)的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12解決antd 下拉框 input [defaultValue] 的值的問題
這篇文章主要介紹了解決antd 下拉框 input [defaultValue] 的值的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-10-10vue2.0實(shí)現(xiàn)前端星星評(píng)分功能組件實(shí)例代碼
本文通過實(shí)例代碼給大家介紹了vue2.0實(shí)現(xiàn)前端星星評(píng)分功能組件,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2018-02-02vue2中使用echarts實(shí)現(xiàn)中國地圖、在中國地圖上標(biāo)注坐標(biāo)散點(diǎn)圖的操作代碼
這篇文章主要介紹了vue2中使用echarts實(shí)現(xiàn)中國地圖、在中國地圖上標(biāo)注坐標(biāo)散點(diǎn)圖,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2024-05-05Vue設(shè)置長時(shí)間未操作登錄自動(dòng)到期返回登錄頁
這篇文章主要介紹了Vue設(shè)置長時(shí)間未操作登錄以后自動(dòng)到期返回登錄頁,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2020-01-01