Vue3中SetUp函數(shù)的參數(shù)props、context詳解
1.setUp函數(shù)的第1個(gè)參數(shù)props
setup(props,context){}
第一個(gè)參數(shù)props:
props是一個(gè)對(duì)象,包含父組件傳遞給子組件的所有數(shù)據(jù)。
在子組件中使用props進(jìn)行接收。
包含配置聲明并傳入的所有的屬性的對(duì)象
也就是說:如果你想通過props的方式輸出父組件傳遞給子組件的值。
你需要使用props進(jìn)行接收配置。即props:{......}
如果你未通過Props進(jìn)行接受配置,則輸出的值是undefined
<template> <div class="box"> 父組件 </div> <no-cont :mytitle="msg" othertitle="別人的標(biāo)題" @sonclick="sonclick"> </no-cont> </template> <script lang="ts"> import NoCont from "../components/NoCont.vue" export default { setup () { let msg={ title:'父組件給子給子組件的數(shù)據(jù)' } function sonclick(msss:string){ console.log(msss) } return {msg,sonclick} }, components:{ NoCont } } </script>
<template> <div @click="sonHander"> 我是子組件中的數(shù)據(jù) </div> </template> <script lang="ts"> import { defineComponent,setup } from 'vue'; export default defineComponent({ name: 'NoCont', // 未進(jìn)行接受 // props:{ // mytitle:{ // type:Object // } // }, setup(props,context){ console.log('props==>',props.mytitle);//輸出的值是 undefined function sonHander(){ context.emit('sonclick','子組件傳遞給父組件') } return {sonHander} } }); </script>
為什么通過props.mytitle輸出的值是undefined呢?
因?yàn)槲覀儧]有使用props進(jìn)行接收配置。即
props:{ mytitle:{ type:Object } },
如果我們添加上接受配置
2.參數(shù)context的講解
第2個(gè)參數(shù):context,是一個(gè)對(duì)象。
里面有attrs(獲取當(dāng)前標(biāo)簽上的所有屬性的對(duì)象)
但是該屬性是props中沒有聲明接收的所有的對(duì)象。
如果你使用props去獲取值,同時(shí)props中你聲明了你要獲取的值
則:獲取的值是undefined
注意點(diǎn):
attrs獲取值是不需要props中沒有聲明接收。
第1個(gè)參數(shù)props獲取值是需要props中聲明接收的
有emit事件分發(fā),(傳遞給父組件需要使用該事件)
有slots插槽
<template> <div @click="sonHander"> 我是子組件中的數(shù)據(jù) </div> </template> <script lang="ts"> import { defineComponent,setup } from 'vue'; export default defineComponent({ name: 'NoCont', props:{ mytitle:{ type:Object } }, setup(props,context){ //輸出{title:父組件傳遞的值} console.log('props==>',props.mytitle); // 輸出別人的標(biāo)題【使用context獲取值,不需要使用props去接受】 console.log('context==> ',context.attrs.othertitle); // 輸出undefined,因?yàn)閏ontext不需要使用props去接受。 console.log('contextmytitle==> ',context.attrs.mytitle); function sonHander(){ context.emit('sonclick','子組件傳遞給父組件') } return {sonHander} } }); </script>
3. 子組件向父組件派發(fā)事件
<template> <div @click="sonHander"> 我是子組件中的數(shù)據(jù) </div> </template> <script lang="ts"> import { defineComponent,setup } from 'vue'; export default defineComponent({ name: 'NoCont', props:{ mytitle:{ type:Object } }, setup(props,context){ function sonHander(){ context.emit('sonclick','子組件傳遞給父組件') } return {sonHander} } }); </script>
4.優(yōu)化事件派發(fā)
我們知道第2個(gè)參數(shù)context是一個(gè)對(duì)象
并且對(duì)象中有三個(gè)屬性attrs,slots,emit
在事件派發(fā)的時(shí)候,直接使用emit就ok了
<template> <div @click="sonHander"> 我是子組件中的數(shù)據(jù) </div> </template> <script lang="ts"> import { defineComponent,setup } from 'vue'; export default defineComponent({ name: 'NoCont', props:{ mytitle:{ type:Object } }, setup(props,{attrs,slots,emit}){ //直接使用emit進(jìn)行事件派發(fā) function sonHander(){ emit('sonclick','子組件傳遞給父組件') } return {sonHander} } }); </script>
5.獲取父組件傳遞的值
我們將使用props參數(shù)獲取值
以及使用attrs獲取值
<template> <hr/> <h2>子組件</h2> <div @click="sonHander"> 我是子組件中的數(shù)據(jù) </div> <h2>使用了props聲明接收==>{{ mytitle }}</h2> <h2>使用參數(shù)attrs獲取==>{{ attrs.othertitle }}</h2> </template> <script lang="ts"> import { defineComponent,setup } from 'vue'; export default defineComponent({ name: 'NoCont', props:{ mytitle:{ type:Object } }, setup(props,{attrs,slots,emit}){ function sonHander(){ emit('sonclick','子組件傳遞給父組件') } return {sonHander,attrs} } }); </script>
附使用setup函數(shù)時(shí)需要注意幾點(diǎn):
- setup函數(shù)的執(zhí)行時(shí)機(jī)是在beforeCreate和created之間
- 由于setup執(zhí)行時(shí)機(jī)是在created之間,所以組件才剛剛被創(chuàng)建,而data和methods還沒初始化好,所以無法在setup中使用data和methods
- setup中this指向undefined
- setup只能是同步的,不能是異步的
總結(jié)
到此這篇關(guān)于Vue3中SetUp函數(shù)的參數(shù)props、context的文章就介紹到這了,更多相關(guān)Vue3 SetUp函數(shù)參數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue-infinite-loading2.0 中文文檔詳解
本篇文章主要介紹了vue-infinite-loading2.0 中文文檔詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04利用HBuilder打包前端開發(fā)webapp為apk的方法
下面小編就為大家?guī)硪黄肏Builder打包前端開發(fā)webapp為apk的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11Vue 通過公共字段,拼接兩個(gè)對(duì)象數(shù)組的實(shí)例
今天小編就為大家分享一篇Vue 通過公共字段,拼接兩個(gè)對(duì)象數(shù)組的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-11-11VUE中對(duì)object.object和object[object]的使用解讀
這篇文章主要介紹了VUE中對(duì)object.object和object[object]的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06vue項(xiàng)目中axios如何捕捉http狀態(tài)碼為401錯(cuò)誤問題
這篇文章主要介紹了vue項(xiàng)目中axios如何捕捉http狀態(tài)碼為401錯(cuò)誤問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03Vue如何實(shí)現(xiàn)el-table多選樣式變?yōu)閱芜x效果
這篇文章主要介紹了Vue如何實(shí)現(xiàn)el-table多選樣式變?yōu)閱芜x效果,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05vue+element實(shí)現(xiàn)錨點(diǎn)鏈接方式
這篇文章主要介紹了vue+element實(shí)現(xiàn)錨點(diǎn)鏈接方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07