uniapp父子组件传值3种方法(props、slot和ref)
作者:1VR
这篇文章主要给大家介绍了关于uniapp父子组件传值的3种方法,方法包括props、slot和ref,最近看到uniapp组件传值的方法,这里记录一下,需要的朋友可以参考下
前言
uniapp,父组件向子组件传值有三种方法,分别为props、slot,和ref
1、props
这个应该是最简单最常用的方法,就是子组件写变量,然后把变量名字在js中进行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
插值比较灵活,可以在任何需要写入的地方进行slot ,slot写入name标签后,在父组件进行插值#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 函数控制
这个是父组件调用子组件的函数进行对子组件进行操作
<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>总结
到此这篇关于uniapp父子组件传值3种方法(props、slot和ref)的文章就介绍到这了,更多相关uniapp父子组件传值内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
