欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 八卦 > uniapp中父子组件的传值

uniapp中父子组件的传值

2025/2/24 23:15:28 来源:https://blog.csdn.net/qq_36158551/article/details/139508848  浏览:    关键词:uniapp中父子组件的传值

1. uniapp中父子组件的传值

1.1. 父子组件的传值

  通过props来实现, 子组件通过props来接收父组件传过来的值

1.1.1. 父组件

<!-- 父组件 -->
<template><view><my-son :title="title"  @sendData="getSonData"></my-son><p>子组件向父组件传递数据:{{sonMsg}}</p></view>
</template>
<script>import mySon from '../../../customComponents/son'export default {components: {mySon},data() {return {title:"我是父组件",sonMsg:''}},methods:{/*** 获取子组件向父组件发送的数据*/getSonData(res){this.sonMsg=res}}}
</script>

在这里插入图片描述

1.1.2. 子组件

  通过props接收父组件中传过来的值
props:[‘list’],

<template><div><p>父传过来的值:{{title}}</p><button @click="sendSonData">子组件向父组件传递数据</button></div>
</template><script>export default {props:{title:{type:String,default:''},},methods:{sendSonData(){var that=this//子组件向父组件传递数据that.$emit("sendData", "我是子组件")}}}
</script>

在这里插入图片描述

1.2. 逻辑梳理

1.2.1. 父组件

(1)第一步:引入子组件

	import mySon from '../../../customComponents/son'

(2)第二步:在components中对子组件进行注册;

	components: {mySon},

(3)第三步:以标签的形式载入;通过数据绑定的形式进行传值~

		<my-son :title="title"  @sendData="getSonData"></my-son>

1.2.2. 子组件

  通过props接收父组件中传过来的值;

        props:{title:{type:String,default:''},},

1.3. 实现在当前页面改变props中定义的值

  官方文档:https://uniapp.dcloud.io/vue-components?id=props
  在父组件中赋值时加上 .sync,在子组件中使用 this.$emit(‘update:title’,“uni-app”)改变值
  .sync 修饰符
  当一个子组件改变了一个 prop 的值时,这个变化也会同步到父组件中所绑定。 .sync 它会被扩展为一个自动更新父组件属性的 v-on 监听器。

 <!-- 父组件 --><template><view><syncA :title.sync="title"></syncA></view></template><script>export default {data() {return {title:"hello vue.js"}}}</script>
<!-- 子组件 --><template><view><view @click="changeTitle">{{title}}</view></view></template><script>export default {props: {title: {default: "hello"},},methods:{changeTitle(){//触发一个更新事件this.$emit('update:title',"uni-app")}}}</script>

1.4. 子组件修改prop的值

1.4.1. 通过sync修饰符配合$emit,update实现

父组件:

<template><child-view :num.sync="num"></child-view>
</template>
<script>import childView from './assembly/child'export default {components: {childView},data() {return {num: 2}}}</script>

子组件:

<template><div><p>父传过来的值:{{num}}</p><button @click="changeNum">加一</button></div>
</template><script>export default {name: 'child',props:{num: {type:Number,default: 0}},methods:{changeNum(){this.$emit("update:num",this.num + 1)}}}
</script>

1.4.2. 通过v-model绑定数据源

父组件:

<template><child-view v-modal="num"></child-view>
</template>
<script>import childView from './assembly/child'export default {components: {childView},data() {return {num: 2}}}</script>

子组件:

<template><div><p>父传过来的值:{{num}}</p><input type="text" :value="value" @input="$emit('input',$event.target.value)"></div>
</template><script>export default {name: 'child',props:["value"]}
</script>

1.4.3. 通过父传子,子调用父方法传参数实现

父组件:

<template><child-view :num="num" @updateNum="updateNum"></child-view>
</template>
<script>import childView from './assembly/child'export default {components: {childView},data() {return {num: 2}},methods: {updateNum(num){this.num = num}}</script>

子组件

<template><div><p>父传过来的值:{{num}}</p><button @click="changeNum">加一</button></div>
</template><script>export default {name: 'child',props:{num: {type:Number,default: 0}},methods:{changeNum(){this.$emit("updateNum",this.num + 1)}}}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词