- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) > web開(kāi)發(fā) > JavaScript >
- Vue3中SetUp函數的參數props、context詳解
setup(props,context){}
第一個(gè)參數props:
props是一個(gè)對象,包含父組件傳遞給子組件的所有數據。
在子組件中使用props進(jìn)行接收。
包含配置聲明并傳入的所有的屬性的對象
也就是說(shuō):如果你想通過(guò)props的方式輸出父組件傳遞給子組件的值。
你需要使用props進(jìn)行接收配置。即props:{......}
如果你未通過(guò)Props進(jìn)行接受配置,則輸出的值是undefined
<template> <div class="box"> 父組件 </div> <no-cont :mytitle="msg" othertitle="別人的標題" @sonclick="sonclick"> </no-cont> </template> <script lang="ts"> import NoCont from "../components/NoCont.vue" export default { setup () { let msg={ title:'父組件給子給子組件的數據' } function sonclick(msss:string){ console.log(msss) } return {msg,sonclick} }, components:{ NoCont } } </script>
<template> <div @click="sonHander"> 我是子組件中的數據 </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>
為什么通過(guò)props.mytitle輸出的值是undefined呢?
因為我們沒(méi)有使用props進(jìn)行接收配置。即
props:{ mytitle:{ type:Object } },
如果我們添加上接受配置
第2個(gè)參數:context,是一個(gè)對象。
里面有attrs(獲取當前標簽上的所有屬性的對象)
但是該屬性是props中沒(méi)有聲明接收的所有的對象。
如果你使用props去獲取值,同時(shí)props中你聲明了你要獲取的值
則:獲取的值是undefined
注意點(diǎn):
attrs獲取值是不需要props中沒(méi)有聲明接收。
第1個(gè)參數props獲取值是需要props中聲明接收的
有emit事件分發(fā),(傳遞給父組件需要使用該事件)
有slots插槽
<template> <div @click="sonHander"> 我是子組件中的數據 </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); // 輸出別人的標題【使用context獲取值,不需要使用props去接受】 console.log('context==> ',context.attrs.othertitle); // 輸出undefined,因為context不需要使用props去接受。 console.log('contextmytitle==> ',context.attrs.mytitle); function sonHander(){ context.emit('sonclick','子組件傳遞給父組件') } return {sonHander} } }); </script>
3. 子組件向父組件派發(fā)事件
<template> <div @click="sonHander"> 我是子組件中的數據 </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è)參數context是一個(gè)對象
并且對象中有三個(gè)屬性attrs,slots,emit
在事件派發(fā)的時(shí)候,直接使用emit就ok了
<template> <div @click="sonHander"> 我是子組件中的數據 </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參數獲取值
以及使用attrs獲取值
<template> <hr/> <h2>子組件</h2> <div @click="sonHander"> 我是子組件中的數據 </div> <h2>使用了props聲明接收==>{{ mytitle }}</h2> <h2>使用參數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>
到此這篇關(guān)于Vue3中SetUp函數的參數props、context的文章就介紹到這了,更多相關(guān)Vue3 SetUp函數參數內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。
Copyright ? 2009-2022 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 特網(wǎng)科技 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 百度云 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站