vue中父子组件之间的通信方式

1、属性往下 事件往上

思路: 父组件把事件函数通过事件绑定的方式传递给子组件 让子组件调用并传递参数

<div id="app">
    <coma></coma>
</div>
<template id="tpl">
    <form>
        <h1>我是组件标题---{{title}}</h1>
        <child1 :change='change'></child1> <!-- 1 给子组件绑定一个属性 把父组件的函数传递过去--->
    </form>
</template>

// 把事件传递给子组件
// 定义组件
Vue.component("coma", {
    template: `#tpl`,
    data: function () {
        return {
            title: ""
        }
    },
    components: {
        // 定义子组件
        child1: {
            template: `<div><input v-model='inp' type='text' @input="a"></div>`, // 3 子组件绑定一个事件
            data: function () {
                return {
                    inp: "我是子组件的内容"
                }
            },
            props: ["change"], // 2 子组件接受这个函数
            methods: {
                a: function () {
                    // 4 调用父组件的方法 把内容给父组件的函数传递过去
                    this.change(this.inp)
                }
            }

        },
    },
    // 父组件的方法
    methods: {
        change: function (data) {
            this.title = data;
            console.log(this.title)
        }
    }
})


new Vue({
    el: "#app",
    data: {

    },
})

2、父组件绑定事件 子组件this.$emit

<div id="app">
     <coma></coma>
</div>
<template id="tpl">
     <form>
         <h1>我是组件标题---{{title}}</h1>
         <child1 @change="change"></child1>  // 1 在父组件里 给子组件绑定一个自定义事件
    </form>
</template> 
 // 把事件传递给子组件
    // 定义组件
    Vue.component("coma", {
        template: `#tpl`,
        data: function () {
            return {
                title: ""
            }
        },
        components: {
            // 定义子组件1
            child1: {
                template: `<div><input v-model='inp' type='text' @input="a"></div>`,
                data: function () {
                    return {
                        inp: "我是子组件的内容"
                    }
                },
                methods: {
                    a: function () {
                        // 2 子组件内通过this.$emit进行事件的触发并传递参数
                        this.$emit("change", this.inp);
                    }
                }

            },
        },
        // 父组件的方法
        methods: {
            change: function (childMsg) {
                //    console.log("这里是父组件的一个函数", childMsg)
                this.title = childMsg;
            }
        }
    })
	new Vue({
	     el: "#app" 
	})

3、事件总线

所谓的事件总线是Vue对观察者模式的一个改进。
用法与观察者模式一模一样。

(1)初始化一个新的Vue实例

var bus = new Vue();

(2)使用它在父组件的mounted的生命周期函数内去注册

Vue.component("父组件", {
   mounted: function() {
      bus.$on("xxx", () => {
        // 这里用什么函数 要取决于你的功能中是否要使用外部的this 
      })
   }  
})

(3)在子组件中触发这个父组件绑定的自定义事件

Vue.component("父组件", {

  	components: {
  		"子组件": {
  			methods: {
  				aaa: function() {
  					bus.$emit("xxx", "参数")
  				}
  			}
  		}
  	
  	}

})

4、关系链

我们如果把组件视作节点的话,原生中,获取节点可以通过节点关系属性。 parentNode、childNodes、nextSibling、previousSibling等属性。
在Vue中,也有对应的组件关系。我们称之为关系链。
获取父组件 this.$parent

获取子组件集合 this.$children; ps: 集合加下标获取对应的组件

获取根组件 this.$root

5、ref属性

我们可以给任意的组件或者元素 绑定ref属性 绑定之后我们就可以在对应的层级中通过this.$refs属性进行获取

<div id="app">
    <div ref="a"></div>
    <button @click="click">点我获取div元素</button>
    <father ref="f" ></father>
</div>

获取:

var vue= new Vue({
    methods: {
        click() {
           console.log(this.$refs)
        }
    }
})

当事件触发时:
在这里插入图片描述
总结:
由父组件向子组件传值: 通过props进行通信;
由子组件向父组件传值:关键在于$emit的应用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 3.0 父子组件之间通讯可以通过 props 和 emit 实现。 1. 父组件向子组件传递数据: 在父组件使用子组件时,可以通过 props 将数据传递给子组件。具体实现如下: ```vue <template> <div> <child-component :message="parentMessage"></child-component> </div> </template> <script> import ChildComponent from './ChildComponent.vue' export default { data() { return { parentMessage: 'Hello from parent' } }, components: { ChildComponent } } </script> ``` 在子组件,可以通过 props 接收父组件传递过来的数据。具体实现如下: ```vue <template> <div> {{ message }} </div> </template> <script> export default { props: { message: String } } </script> ``` 2. 子组件向父组件传递数据: 在子组件,可以使用 emit 触发一个自定义事件,并向父组件传递数据。具体实现如下: ```vue <template> <div> <button @click="sendMessage">Send Message</button> </div> </template> <script> export default { data() { return { message: 'Hello from child' } }, methods: { sendMessage() { this.$emit('message', this.message) } } } </script> ``` 在父组件使用子组件时,可以通过 v-on 监听子组件触发的自定义事件,并在事件处理函数获取子组件传递过来的数据。具体实现如下: ```vue <template> <div> <child-component @message="handleMessage"></child-component> <div>{{ receivedMessage }}</div> </div> </template> <script> import ChildComponent from './ChildComponent.vue' export default { data() { return { receivedMessage: '' } }, components: { ChildComponent }, methods: { handleMessage(message) { this.receivedMessage = message } } } </script> ``` 以上就是 Vue 3.0 父子组件之间通讯的实现方式

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值