Home > Article > Web Front-end > Summary of VueJs parent-child component communication methods
This time I will bring you a summary of the communication methods of VueJs parent-child components. What are the precautions for VueJs parent-child component communication? The following is a practical case, let's take a look.
Component (Father-Child Communication)
1. Summary
Define another component within a component, which is called Parent-child components.
But it should be noted that: 1. Child components can only be used inside the parent component (written in the parent component template); The data on, the scope of each component instance is independent;
How to complete the communication between father and son, in a simple sentence: props down, events up: the parent component passes data downward to the child component through props , the child component sends to the parent component through events
Pass from parent to child: Props
Pass from child to parent: Child: $emit(eventName) Parent $on(eventName) Parent accesses child: ref
Let’s explain the three cases below:
2. Pass from father to son: Props The scope of the component instance is isolated. This means that you cannot (and should not) reference the parent component's data directly within the child component's template. To allow the child component to use the data of the parent component, you need to use the props option of the child component
Using Prop to transfer data includes static and dynamic forms. Let’s first introduce static props
1, static props
Effect:
## Naming convention:
2. Dynamic props
var childNode = { template: '3. Passing numbersA common mistake beginners make is to use literal syntax to pass values{{myMessage}}
', props: ['my-message'] } var parentNode = { template: ``, components: { 'child': childNode }, data() { return { 'data1': '111', 'data2': '222' } } };
Result:
Because it is a literal prop, its value is String
expressioncalculation How to convert String to number? In fact, you only need to change one place.
var parentNode = { template: `Of course, if you want to pass a string type through v-bind, what should you do? We can use dynamic props and set the corresponding number in the data attribute 1//只要把父组件my-message="1"改成:my-message="1"结果就变成number类型
`, };
var parentNode = { template: `3. Transfer from child to parent: $emit`, components: { 'myChild': childNode }, //这里'data': 1代表就是number类型,'data': "1"那就代表String类型 data(){ return { 'data': 1 } } };
About the usage of $emit
1. The parent component can use props to pass data to the child component.2. Subcomponents can use $emit to trigger custom events of parent components.
Child primary key
Parent component大连
The result is: toCity: DalianSecond case//监听子组件的showCityName事件。
Detailed explanation:
{{ total }}
1:button-counter作为父主键,父主键里有个button按钮。
2:两个button都绑定了click事件,方法里: this.$emit('increment1', [12, 'kkk']);,那么就会去调用父类v-on所监听的increment1事件。
3:当increment1事件被监听到,那么执行incrementTotal,这个时候才会把值传到父组件中,并且调用父类的方法。
4:这里要注意第二个button-counter所对应的v-on:'increment2,而它里面的button所对应是this.$emit('increment1', [12, 'kkk']);所以第二个button按钮是无法把值传给他的父主键的。
示例:一个按钮点击一次那么它自身和上面都会自增1,而第二个按钮只会自己自增,并不影响上面这个。
还有就是第一个按钮每点击一次,后台就会打印一次如下:
四、ref ($refs)用法
ref 有三种用法
1.ref 加在普通的元素上,用this.ref.name 获取到的是dom元素
2.ref 加在子组件上,用this.ref.name 获取到的是组件实例,可以使用组件的所有方法。
3.如何利用v-for 和ref 获取一组数组或者dom 节点
1.ref 加在普通的元素上,用this.ref.name 获取到的是dom元素
ref在外面的组件上
效果:当在p访问内点击一次:
2.ref使用在外面的元素上
ref在外面的元素上
效果:当在p访问内点击一次:
3.ref使用在里面的元素上---局部注册组件
ref在里面的元素上
效果:当在click范围内点击一次:
4.ref使用在里面的元素上---全局注册组件
效果:当我第一次输入1时,值已改变出发事件,当我第二次在输入时在触发一次事件,所以后台应该打印两次
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
The above is the detailed content of Summary of VueJs parent-child component communication methods. For more information, please follow other related articles on the PHP Chinese website!