上篇提到slot
传入的内容都是由外层元件提供,如果内层元件slot
也想使用内层资料时,就可以使用Slot Props
,概念就是内层元件利用插槽的props
将部份内层资料,传递给外层元件做使用。
Slot Props
参考以下程序码:
<div id="app">
<out-text>
<template v-slot:default="roprops">
内层元件插槽:
{{ roprops.ro.name }}
</template>
</out-text>
</div>
const app = Vue.createApp({
data() {
return {
text: "外层元件",
}
}
});
app.component("outText",{
template: `
<div class="header">
<slot :ro="product"></slot>
</div>
`,
data() {
return {
text: "内部元件",
product: {
name: "名刀不知火",
price: 200000,
city: "普隆德拉"
}
};
}
});
app.mount("#app");
<slot :ro="product"></slot>
,:ro
为自定义名称,product
为内层元件的data
。v-slot:default="roprops"
接收资料,v-slot:default
固定写法,roprops
为自定义名称。Slot Props
搭配Component Props
<div id="app">
<out-text :product="product">
<template v-slot:default="{ ro, buy }">
component props:{{ ro.name }} <br>
slot props:{{ buy }}
</template>
</out-text>
</div>
const app = Vue.createApp({
data() {
return {
text: "外层元件",
product: {
name: "名刀不知火",
price: 200000,
city: "普隆德拉",
amount: 5,
}
}
}
});
app.component("outText",{
props: ['product'],
template: `
<div class="header">
<slot :ro="product" :buy="buy"></slot>
</div>
`,
data() {
return {
text: "内部元件",
buy: ""
};
},
created() {
if(this.product.amount > 1 ){
this.buy = "可购买";
}else {
this.buy = "无法购买";
};
}
});
app.mount("#app");
props
到子元件使用,<out-text :product="product">
,props: ['product']
。mounted()
。<slot :ro="product" :buy="buy"></slot>
(资料是外部元件data
,经由内部元件props
提供)。v-slot:default="{ ro, buy }"
。
哈罗大家好,打ㄍㄟ厚,我是目前就读天大地大台科大的 Steven Meow,这是我第一次参加铁人赛,...
本系列文之後也会置於个人网站 接着来看看爲什麽更新帐号资讯,在「快速开始」会有那些变化。 这与cl...
鬼故事 - CS 高手 Credit: Vince mcmahon 灵感来源: UCCU Hacke...
前言 我们今天要试着解决昨天阅读 WhenAll 留下来的两个问题 为何要 "atomic...
Understand responsible AI Fairness - Without incor...