uniapp父子組件傳值3種方法(props、slot和ref)
前言
uniapp,父組件向子組件傳值有三種方法,分別為props、slot,和ref
1、props
這個(gè)應(yīng)該是最簡(jiǎn)單最常用的方法,就是子組件寫(xiě)變量,然后把變量名字在js中進(jìn)行props
<template>
<view>
<!-- 我是子組件 newzujian-->
<view class="">
{{value}}
</view>
</view>
</template>
<script>
export default {
props:['value'],
methods:{
}
}
</script><template>
<view>
<!-- 我是父組件 -->
<newzujian value='789' >
</newzujian>
</view>
</template>
<script>
export default {
methods: {
}
}
</script>2、slot
插值比較靈活,可以在任何需要寫(xiě)入的地方進(jìn)行slot ,slot寫(xiě)入name標(biāo)簽后,在父組件進(jìn)行插值#name
<template>
<view>
<!-- 我是子組件 newzujian-->
<view class="">
<slot name="value"></slot>
</view>
</view>
</template>
<script>
export default {
methods:{
}
}
</script> <template>
<view>
<!-- 我是父組件 -->
<newzujian >
<template #value>
789
</template>
</newzujian>
</view>
</template>
<script>
export default {
methods: {
}
}
</script>3、ref 函數(shù)控制
這個(gè)是父組件調(diào)用子組件的函數(shù)進(jìn)行對(duì)子組件進(jìn)行操作
<template>
<view>
<!-- 我是子組件 newzujian-->
<view class="">
{{value}}
</view>
</view>
</template>
<script>
export default {
data(){
return{
value:''
}
},
methods:{
gaibian(){
this.value='789'
}
}
}
</script> <template>
<view>
<!-- 我是父組件 -->
<newzujian ref="hanshu" >
</newzujian>
<button @click="dianji">click</button>
</view>
</template>
<script>
export default {
onLoad() {
},
methods: {
dianji(){
this.$refs.hanshu.gaibian()
}
}
}
</script>總結(jié)
到此這篇關(guān)于uniapp父子組件傳值3種方法(props、slot和ref)的文章就介紹到這了,更多相關(guān)uniapp父子組件傳值內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
javascript中的=等號(hào)個(gè)數(shù)問(wèn)題兩個(gè)跟三個(gè)有什么區(qū)別
一個(gè)等號(hào)就是個(gè)賦值的作用,主要問(wèn)題在于兩個(gè)跟三個(gè)等號(hào)的區(qū)別,想必有很多的朋友都不知道吧,在本文有個(gè)不錯(cuò)的示例主要介紹下兩者到底有什么區(qū)別,感興趣的朋友不要錯(cuò)過(guò)2013-10-10
JavaScript中的getTimezoneOffset()方法使用詳解
這篇文章主要介紹了JavaScript中的getTimezoneOffset()方法使用詳解,是JS入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-06-06
iframe 上下滾動(dòng)條如何默認(rèn)在下方實(shí)現(xiàn)原理
iframe 上下滾動(dòng)條如何默認(rèn)在下方,做的是聊天工具,數(shù)據(jù)多了,每次刷新出現(xiàn)的上下滾動(dòng)默認(rèn)在上方,還需下拉到下面才能看到聊天記錄,本文將介紹,如和實(shí)現(xiàn)在下方2012-12-12
Dojo Javascript 編程規(guī)范 規(guī)范自己的JavaScript書(shū)寫(xiě)
良好的JavaScript書(shū)寫(xiě)習(xí)慣的優(yōu)點(diǎn)不言而喻,今天彬Go向大家推薦Dojo Javascript 編程規(guī)范,相當(dāng)不錯(cuò)的 Javascript 編程風(fēng)格規(guī)范,建議大家可以借鑒一下此規(guī)范編寫(xiě) Javascript。感謝i.feelinglucky的翻譯2014-10-10

