美文网首页
uniapp-组件通讯

uniapp-组件通讯

作者: hszz | 来源:发表于2021-08-21 01:31 被阅读0次

1.父传子

prop
  • 属性绑定, 子组件通过prop属性childrenData可以接受到父组件中绑定的值:childrenData
<!-- 父组件 hsz—test -->
<template>
    <view>
        <view class="">
            <view class="">
                父组件fatherData:{{fatherData}}
            </view>
            <!-- 子组件 -->
            <hsz-test-component :childrenData="fatherData"></hsz-test-component>
        </view>
    </view>
</template>

<script>
    export default {
        data() {
            return {
                fatherData: 'hszz-fatherData',
            }
        },
    }
</script>
<!-- 子组件 hsz-test-component -->
<template>
    <view>
        <view class="">
            <view>子组件childrenData: {{childrenData}}</view>
        </view>
    </view>
</template>

<script>
    export default {
        name: "hsz-test-component",
        props: {
            childrenData: {
                type: String, // 类型
                default: '', // 默认值
            }
        },
    }
</script>

2.子传父

this.$emit(funcName, val)
  • 事件传递,funcName:事件名, val:值
<!-- 父组件 hsz—test -->
<template>
    <view>
        <view class="">
            <!-- 绑定子组件事件,接收子组件值 -->
            <hsz-test-component @handleChange="changeName"></hsz-test-component>
            <view class="">
                {{name}}
            </view>
        </view>
    </view>
</template>

<script>
    export default {
        data() {
            return {
                name:'',
            }
        },
        methods: {
            changeName(name) { // name形参是子组件中传入的值
                this.name = name
            }
        }
    }
</script>


<!-- 子组件 hsz-test-component -->
<template>
    <view>
        <!-- 子组件设置 -->
        <view class="">
            子组件
        </view>
    </view>
</template>

<script>
    export default {
        name: "hsz-test-component",
        data() {
            return {
                name: 'hszz'
            };
        },
        mounted() {
            // 通过事件传递给父组件
            this.$emit('handleChange', '我是子组件传给父组件的值:' + this.name)
        }
    }
</script>
image.png

3.

$refs $children $parent
  • 父组件访问子组件
    • this.$children[0]

      • this.$children是一个数组,包含使用子组件,可通过索引选择组件
    • this.$refs.comhsz

  • 子组件访问父组件
    • this.$parent
<!-- 父组件 hsz—test -->
<template>
    <view>
        <view class="">
            <view class="">
                父组件 {{name}}
            </view>
            <hsz-test-component ref="comhsz"></hsz-test-component>
        </view>
    </view>
</template>

<script>
    export default {
        data() {
            return {
                name: '',
            }
        },
        methods: {
            changeName() {
                this.name = '子组件调用父组件方法 hszz-c'
            },

            // 父组件调用子组件两种方法
            // this.$children
            fatherChange() {
                this.$children[0].changeName()
            },
            // this.$ref 需要在子组件设置ref="comhsz"
            fatherChangeRef() {
                this.$refs.comhsz.changeName()
            }
        },
        mounted() {
            //this.fatherChange()
            this.fatherChangeRef()
        }
    }
</script>

<!-- 子组件 hsz-test-component -->
<template>
    <view>
        <!-- 子组件设置 -->
        <view class="">
            子组件 {{cName}}
        </view>
    </view>
</template>

<script>
    export default {
        name: "hsz-test-component",
        data() {
            return {
                cName: ''
            };
        },
        methods: {
            changeName() {
                this.cName = '父组件调用子组件方法 hszz-c'
            },
            
            // 子组件调用父组件方法
            childrenChange() {
                this.$parent.changeName()
            }
        },
        mounted() {
            this.childrenChange()
        }
    }
</script>
image.png

相关文章

  • uniapp-组件通讯

    1.父传子 prop 属性绑定, 子组件通过prop属性childrenData可以接受到父组件中绑定的值:chi...

  • vue组件通信

    1.组建通讯---父子组件通讯 父子通信通过props属性进行传值 父组件 子组件 1.组建通讯---子父组件通讯...

  • uniapp-小程序-瀑布流布局-支持下拉刷新上拉加载更多

    uniapp-小程序-瀑布流布局-支持下拉刷新上拉加载更多 主要思路: 将父组件传递进来的list分为两组,lef...

  • Vue——组件通讯

    组件通讯 一、组件:组件是可复用的 Vue 实例 二、创建组件: 例如: 三、组件通讯 父传子:1.创建两个组件A...

  • 7天深入Vue - vue组件之间的通讯与插槽(一)

    常用组件之间的通讯 1.props vue 基础父子组件通讯 数组形式 子组件定义props:['title', ...

  • 组件通讯

    1.什么是组件 组件 (Component) 是 Vue.js 最强大的功能之一。组件可以扩展 HTML 元素,封...

  • 组件通讯

    http://taobaofed.org/blog/2016/11/17/react-components-com...

  • 组件通讯

    2017年46周学习总结 angular组件间通讯 组件输入输出 当子组件需要向父组件传递信息时需要用到输出属性,...

  • vue组件如何通信?有几种方式?

    在vue中组件通讯可以分为父子组件通讯和非父子组件通信。父组件通过props的方式向子组件传递数据,而子组件可以通...

  • vue2-父子组件通信

    父组件到子组件通讯 父组件到子组件的通讯主要为:子组件接受使用父组件的数据,这里的数据包括属性和方法(String...

网友评论

      本文标题:uniapp-组件通讯

      本文链接:https://www.haomeiwen.com/subject/gyfciltx.html