setup(props,context){}
第一個參數props:
props是一個對象,包含父組件傳遞給子組件的所有資料。
在子組件中使用props進行接收。
包含配置聲明並傳入的所有的屬性的物件
也就是說:如果你想透過props的方式輸出父元件傳遞給子元件的值。
你需要使用props進行接收設定。即props:{......}
如果你未透過Props進行接受配置,則輸出的值是undefined
<template>
<div>
父組件</div>
<no-cont :mytitle="msg"
othertitle="別人的標題"
@sonclick="sonclick">
</no-cont>
</template>
<script>
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>
import { defineComponent,setup } 從 'vue';
export default defineComponent({
name: 'NoCont',
// 未進行接受// props:{
// mytitle:{
// type:Object
// }
// },
setup(props,context){
console.log('props==>',props.mytitle);//輸出的值是undefined
function sonHander(){
context.emit('sonclick','子元件傳遞給父元件')
}
return {sonHander}
}
});
</script>
為什麼透過props.mytitle輸出的值是undefined呢?
因為我們沒有使用props進行接收配置。即
props:{
mytitle:{
type:Object
}
},如果我們加入上接受配置
第2個參數:context,是一個物件。
裡面有attrs(取得目前標籤上的所有屬性的物件)
但是該屬性是props中沒有聲明接收的所有的物件。
如果你使用props去取得值,同時props中你聲明了你要取得的值
則:取得的值是undefined
注意點:
attrs取得值是不需要props中沒有聲明接收。
第1個參數props取得值是需要props中聲明接收的
有emit事件分發,(傳遞給父元件需要使用該事件)
有slots插槽
<template>
<div @click="sonHander">
我是子組件中的資料</div>
</template>
<script>
import { defineComponent,setup } 從 '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. 子元件向父元件派發事件
<template>
<div @click="sonHander">
我是子組件中的資料</div>
</template>
<script>
import { defineComponent,setup } 從 'vue';
export default defineComponent({
name: 'NoCont',
props:{
mytitle:{
type:Object
}
},
setup(props,context){
function sonHander(){
context.emit('sonclick','子元件傳遞給父元件')
}
return {sonHander}
}
});
</script> 4.優化事件派發
我們知道第2個參數context是一個物件
並且物件中有三個屬性attrs,slots,emit
在事件派發的時候,直接使用emit就ok了
<template>
<div @click="sonHander">
我是子組件中的資料</div>
</template>
<script>
import { defineComponent,setup } 從 'vue';
export default defineComponent({
name: 'NoCont',
props:{
mytitle:{
type:Object
}
},
setup(props,{attrs,slots,emit}){
//直接使用emit進行事件派發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>
import { defineComponent,setup } 從 'vue';
export default defineComponent({
name: 'NoCont',
props:{
mytitle:{
type:Object
}
},
setup(props,{attrs,slots,emit}){
function sonHander(){
emit('sonclick','子元件傳遞給父元件')
}
return {sonHander,attrs}
}
});
</script>
以上就是詳解vue3中setup函數的參數-props和context的詳細內容,更多請關注php中文網其它相關文章!
