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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | <script src= "https://unpkg.com/vue" ></script>
<p id= "example" >
<parent></parent>
</p>
<script>
var childNode = {
template: '<p>{{message}}</p>' ,
props: [ 'message' ]
}
var parentNode = {
template: `
<p class = "parent" >
<child message= "我是" ></child>
<child message= "徐小小" ></child>
</p>`,
components: {
'child' : childNode
}
};
new Vue({
el: '#example' ,
components: {
'parent' : parentNode
}
})
</script>
|
Copy after login
Effect:
## Naming convention: 
For attributes declared by props, in the parent HTML template, the attribute name It is necessary to use the dash writing method
When the child props attribute is declared, you can use the small camel case or the dash writing method; when the child template uses variables passed from the parent, you need to use the corresponding small camel case Writing method
What does the above sentence mean?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <script>
var childNode = {
template: '<p>{{myMessage}}</p>' ,
props: [ 'myMessage' ]
}
var parentNode = {
template: `
<p class = "parent" >
<child my-message= "我是" ></child>
<child my-message= "徐小小" ></child>
</p>`,
components: {
'child' : childNode
}
};
</script>
|
Copy after login
If myMessage in our childNode is changed to {{my-message}}, look at the running results:
2. Dynamic props
In the template, you need to dynamically bind the data of the parent component to the props of the child template, which is similar to binding to any ordinary HTML feature, that is, use v-bind. Whenever the data of the parent component changes, the change will also be transmitted to the child component
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | var childNode = {
template: '<p>{{myMessage}}</p>' ,
props: [ 'my-message' ]
}
var parentNode = {
template: `
<p class = "parent" >
<child :my-message= "data1" ></child>
<child :my-message= "data2" ></child>
</p>`,
components: {
'child' : childNode
},
data() {
return {
'data1' : '111' ,
'data2' : '222'
}
}
};
|
Copy after login
3. Passing numbers
A common mistake beginners make is to use literal syntax to pass values
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <script src= "https://unpkg.com/vue" ></script>
<p id= "example" >
<parent></parent>
</p>
<script>
var childNode = {
template: '<p>{{myMessage}}的类型是{{type}}</p>' ,
props: [ 'myMessage' ],
computed: {
type() {
return typeof this.myMessage
}
}
}
var parentNode = {
template: `
<p class = "parent" >
<my-child my-message= "1" ></my-child>
</p>`,
components: {
'myChild' : childNode
}
};
new Vue({
el: '#example' ,
components: {
'parent' : parentNode
}
})
</script>
|
Copy after login
Result:
Because it is a literal prop, its value is
String
"1" instead of number. If you want to pass an actual number, you need to use v-bind so that its value is treated as a JS
expressioncalculation How to convert String to number? In fact, you only need to change one place.
1 2 3 4 5 6 7 | var parentNode = {
template: `
<p class = "parent" >
<my-child :my-message= "1" ></my-child>
</p>`,
};
|
Copy after login
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | var parentNode = {
template: `
<p class = "parent" >
<my-child :my-message= "data" ></my-child>
</p>`,
components: {
'myChild' : childNode
},
data(){
return {
'data' : 1
}
}
};
|
Copy after login
3. Transfer from child to parent: $emit
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <template>
<p class = "train-city" >
<span @click='select(`大连`)'>大连</span>
</p>
</template>
<script>
export default {
name: 'trainCity' ,
methods:{
select(val) {
let data = {
cityname: val
};
this. $emit ( 'showCityName' ,data);
}
}
}
</script>
|
Copy after login
Parent component
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <template>
<trainCity @showCityName= "updateCity" :index= "goOrtoCity" ></trainCity>
<template>
<script>
export default {
name: 'index' ,
data () {
return {
toCity: "北京"
}
}
methods:{
updateCity(data){
this.toCity = data.cityname;
console.log( 'toCity:' +this.toCity)
}
}
}
</script>
|
Copy after login
The result is: toCity: Dalian
Second case
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | <script src= "https://unpkg.com/vue" ></script>
<p id= "counter-event-example" >
<p>{{ total }}</p>
<button-counter v-on:increment1= "incrementTotal" ></button-counter>
<button-counter v-on:increment2= "incrementTotal" ></button-counter>
</p>
<script>
Vue.component( 'button-counter' , {
template: '<button v-on:click="increment">{{ counter }}</button>' ,
data: function () {
return {
counter: 0
}
},
methods: {
increment: function () {
this.counter += 1;
this. $emit ( 'increment1' , [12, 'kkk' ]);
}
}
});
new Vue({
el: '#counter-event-example' ,
data: {
total: 0
},
methods: {
incrementTotal: function (e) {
this.total += 1;
console.log(e);
}
}
});
</script>
|
Copy after login
Detailed explanation:
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元素
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <script src= "https://unpkg.com/vue" ></script>
<p id= "ref-outside-component" v-on:click= "consoleRef" >
<component-father ref= "outsideComponentRef" >
</component-father>
<p>ref在外面的组件上</p>
</p>
<script>
var refoutsidecomponentTem = {
template: "<p class='childComp'><h5>我是子组件</h5></p>"
};
var refoutsidecomponent = new Vue({
el: "#ref-outside-component" ,
components: {
"component-father" : refoutsidecomponentTem
},
methods: {
consoleRef: function () {
console.log(this.);
console.log(this. $refs .outsideComponentRef);
}
}
});
</script>
|
Copy after login
效果:当在p访问内点击一次:

2.ref使用在外面的元素上
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <script src= "https://unpkg.com/vue" ></script>
<!--ref在外面的元素上-->
<p id= "ref-outside-dom" v-on:click= "consoleRef" >
<component-father>
</component-father>
<p ref= "outsideDomRef" >ref在外面的元素上</p>
</p>
<script>
var refoutsidedomTem = {
template: "<p class='childComp'><h5>我是子组件</h5></p>"
};
var refoutsidedom = new Vue({
el: "#ref-outside-dom" ,
components: {
"component-father" : refoutsidedomTem
},
methods: {
consoleRef: function () {
console.log(this);
console.log(this. $refs .outsideDomRef);
}
}
});
</script>
|
Copy after login
效果:当在p访问内点击一次:

3.ref使用在里面的元素上---局部注册组件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <script src= "https://unpkg.com/vue" ></script>
<!--ref在里面的元素上-->
<p id= "ref-inside-dom" >
<component-father>
</component-father>
<p>ref在里面的元素上</p>
</p>
<script>
var refinsidedomTem = {
template: "<p class='childComp' v-on:click='consoleRef'>" +
"<h5 ref='insideDomRef'>我是子组件</h5>" +
"</p>" ,
methods: {
consoleRef: function () {
console.log(this);
console.log(this. $refs .insideDomRef);
}
}
};
var refinsidedom = new Vue({
el: "#ref-inside-dom" ,
components: {
"component-father" : refinsidedomTem
}
});
</script>
|
Copy after login
效果:当在click范围内点击一次:

4.ref使用在里面的元素上---全局注册组件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <script src= "https://unpkg.com/vue" ></script>
<!--ref在里面的元素上--全局注册-->
<p id= "ref-inside-dom-all" >
<ref-inside-dom-quanjv></ref-inside-dom-quanjv>
</p>
<script>
Vue.component( "ref-inside-dom-quanjv" , {
template: "<p class='insideFather'> " +
"<input type='text' ref='insideDomRefAll' v-on:input='showinsideDomRef'>" +
" <p>ref在里面的元素上--全局注册 </p> " +
"</p>" ,
methods: {
showinsideDomRef: function () {
console.log(this);
console.log(this. $refs .insideDomRefAll);
}
}
});
var refinsidedomall = new Vue({
el: "#ref-inside-dom-all"
});
</script>
|
Copy after login
效果:当我第一次输入1时,值已改变出发事件,当我第二次在输入时在触发一次事件,所以后台应该打印两次

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
怎样用JS跨域实现POST
怎么用Vue实现树形视图数据
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!