Home > Web Front-end > JS Tutorial > body text

The method of parent-child communication in VueJs components

不言
Release: 2018-05-07 14:57:36
Original
1191 people have browsed it

This article mainly introduces the method of father-son communication in VueJs components. It is very good and has certain reference value. Friends in need can refer to

Component (Father-Child Communication)

1. Summary

Define another component within a component, called a parent-child component.

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


Copy after login

Effect:

## Naming convention:

For attributes declared by props For example, in the parent HTML template, the attribute name needs to be written in underscore

When the child props attribute is declared, it can be written in camel case or underscore; while the child template uses the underscore from the parent When passing variables, you need to use the corresponding camel case

What does the above sentence mean?

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, using v-bind. Whenever the data of the parent component changes, the change will also be transmitted to the child component

 var childNode = {
  template: '

{{myMessage}}

', props: ['my-message'] } var parentNode = { template: `

`, components: { 'child': childNode }, data() { return { 'data1': '111', 'data2': '222' } } };
Copy after login

3. Passing numbers

Common mistakes made by beginners One mistake is to use literal syntax to pass a numeric value


Copy after login

The result:

## because it is a Literal prop, its value is the string "1" instead of number. If you want to pass an actual number, you need to use v-bind so that its value is calculated as a JS expression

How to convert String to number? In fact, you only need to change one place.

 var parentNode = {
  template: `
 

//只要把父组件my-message="1"改成:my-message="1"结果就变成number类型

`, };
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 to set the corresponding number in the data attribute 1

var parentNode = {
 template: `
 

`, components: { 'myChild': childNode }, //这里'data': 1代表就是number类型,'data': "1"那就代表String类型 data(){ return { 'data': 1 } } };
Copy after login

3. Sub-turn 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


 
Copy after login

Parent component