Vue2.0 three common transmission modes (Father, sub-ferry, non-father sub-component pass)

Vue2.0 Passivity Method:

During the project of Vue development, the components are often used to manage different functions, and some public components will be Extract. Do you inevitably produce some questions and needs? For example, a component calls another component as its own subcomponent, then how do we perform a pass value to subcomponents? If it is the development of the e-commerce website system, it will also involve the option of the shopping cart, which occurs when the non-father subcomponent is transmitted. Of course, you can also implement it with the Vuex status management tool. This part we will be introduced separately. I will introduce you to the three types of patented in Vue development.

VUE commonly used three types of passages:

• Father Chronicles

• Sub-passfather

• Non-parent-child pass value

Reference a sentence of the official website: The relationship between the parent subcomponents can be summarized as PROP, and the event is transmitted upward. The parent component issues data to the child component via the Prop, and the sub-component sends a message to the parent component, as shown in the following figure:

Vue2.0中三种常用传值方式(父传子、子传父、非父子组件传值) Next, we can see the examples It will be more understood:

1. Parent components transmit values ​​to subcails

Vue2.0中三种常用传值方式(父传子、子传父、非父子组件传值) parent components:


Import Child from './child' Export Default. ../CHILD 'EXPORT DEFAULT {Components: {CHILD}, DATA () {Return {Name: ''}}}
Children: {{INPUTNAME}}

EXPORT DEFAULT {// Accept the value of the parent component PROPS: { INPUTNAME: STRING, Required: true}}
  2. Subface to the parent components  
Childrenic assembly:

Vue2.0中三种常用传值方式(父传子、子传父、非父子组件传值) export default {data () {return {childValue: ‘I am a sub-component data’}}}, methods: {childClick () {// childbyvalue is the method // second parameter listening in the parent component ON This.childValue is a value of the value this. $ EMIT (‘ChildbyValue’, this.childValue)}}}

 Parent component:   
Parent component: {Name}}

Import Child from ‘./child’ Export Default{Components: {CHILD} () {return {name: ”}}, methods: {childbyvalue: function (childue) {// childValue is the value of subcomponents through the value = childValue}}}

 3. Non-parent subcomponents perform transmission value   The value between the non-parent subcomponents, you need to define a public public instance file bus.js, As the intermediate warehouse, the value is not met between the routing components. 

// Bus.jsimport Vue from 'Vue'Export Default New Vue ()
A component: {{ElementValue}}

// Introduces a common bug to do the tool import bus from ‘./bus.js’ export default {data () {return {ElementValue: 4}}, methods: {ElementByvalue: function () {bus. $ EMIT (‘Val’, this.elementValue)}}}

B component:

Import bus from ‘./bus.js’ export default {data () {return {name: 0}}, mounted: function () {var vm = this // with $ on events Receive parameters BUS. $ ON (‘Val’, (DATA) => {Console.log (data) VM.Name = DATA})}, methods: {getData: function () { ++}}
Summary The above is the three common transmission modes in the Vue2.0 introduced by Xiaobian. Child, sub-parent, non-father subcomponent, I hope to help everyone, if you have any questions, please leave a message, Xiaobian will reply to everyone in time. Thank you very much for your support of Tumi Cloud Website!
© Copyright Notice
Just support it if you like
comment Grab the couch

Please log in to comment