Vue's pass value

tags: javascript  vue.js

Vue's pass value:

1. Father:

Bind the data to be passed through the V-bind of the sub-component tag of the parent component, and then receive in the sub-component through the PROPS

// parent component
<select-list :dataList="dataArr" @parentReceive="changeVal"/>
    
 // Subassembly 
props: ['dataList']    

12345

2. Submail:

Define a method in subcomponents, then pass through This.emit (EMIT delivery (EMIT has two parameters (data to be transferred)):

// SELECT-LIST component - subcomponents
selectVal (val) {
             // First step
             THIS. $ EMIT ('ParentReceive', VAL) // First Parameters: Custom Event Name; Second Parameter To pass the data
}
12345

Listen to the @ Custom Event Name on the subcomponent tag of the parent component, then derive the logic of the response by the callback function:

// parent component
<template>
	<select-list :dataList="dataArr" @parentReceive="changeVal"/>
 </template>

 <script>
 	 changeVal (val) {
             Console.log (`I am the data passed by the subcomponent $ {VAL}`)
    }  
 </script>
12345678910

3. Central Communication (crossable communication):

In the component to pass the data passes the $ EMIT (custom event name, the data to be passed):

// select - list
import bus from '../../main'
selectVal (val) {
      bus.$emit('reveive', val)
    }
12345

Listening to the reception ON by ON to monitor the reception ON to monitor the reception ON ('Custom Event Name ":

// select - input
import bus from '../../main'
bus.$on('reveive', val => {
      console.log(val)
      this.value = val
 })
123456

All $ EMIT and $ ON are mounted to the Vue empty instance, and this empty instance should be used globally and therefore need to be created in main.js:

// main.js
let bus = new Vue()
export default bus

Use VUEX to pass parameters

The first is to define a property name in State in Vuex.

state:{
  value:""
}

Write in a component

methods:{
  sendvalue(){
	this.$store.dispatch("send","value")
}
}

Write in Vuex

actions:{
  send(context,valuename){
    this.context("sendover",valuename)
  }
}
mutations:{
  sendover(state,valuename){
    this.state.value=valuename
  }
}

Accept in another component

var a = this.$store.state.value

Intelligent Recommendation

Vue's url pass

Define the parameters to be passed in router.js The url of the About in App.vue plus the parameter value Get the parameter value passed by App.vue in About.vue...

Vue's jump, pass parameters,

First, how to jump into the parametric reference this.$router.push() Used within the method (Methods: {}), such as a click time and other methods to trigger a jump Instructions Without parameters Para...

Vue's first 2 types of pass

There are 3 types of values ​​commonly used by VUE. Father 2. Son 3. Submid Father The relationship between the parent subcomponents can be summarized as PROP, and the event is transmitted upward. The...

Vue's way of passing value

The three commonly used methods of passing values ​​in Vue are: 1. Father to Son 2. Son passes to father 3. Non-parent and child pass by value Let me talk about two of them first Father to son and son...

Pass by value and pass by reference

[size=large][color=red][b]Java only passes by value, not by reference! [/b][/color][/size] Running result : ------> The value of x is: 0 Running result: meili beautiful Result: ------> The value...

More Recommendation

Pass value and pass reference

Go can be seen as an enhanced version of the C language, so it also continues some of C's features to some extent. Same as C in Go languageFunction call parameterAll are passed by value, including all...

Reference pass and value pass

  Reference pass and value pass Here we need to use the concepts of actual parameters and formal parameters to help understand, Value passing: When the method is called, the actual parameter pass...

Value pass or reference pass

Javascript is passed by value The parameter passing of the javascript function, the value is passed, the parameter is the same as the Object type, but also the value, but this value is the address val...

Pass by value and pass by address

The case of addressing is as follows: When the parameters of the function are passed into an object, it is the address, thinking that the object stores the address So, if you declare a global variable...

Pass by value or pass by reference

Pass by value or pass by reference? When I was very tender, I once thoughtJavaScript The parameter passing method of the function in the function is: When the parameter is a basic type, it is passed b...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top