EventBus transfer parameter assignment between vue pages does not work

pass parameters between vue pages, but the assignment is not successful.

1. The effect you want to achieve:
Page A will enter page B after a button is pressed. Clicking on the list item in page B will return to page An and bring back the data of the list item

.

2. Procedure:

  • create eventBus

    import Vue from "vue"
    var eventBus=new Vue({});
    export default eventBus;
  • Page B
import eventBus from "eventBus.js";
//methods
 backData(item){
         eventBus.$emit("choiceAddress"item);
             this.$router.go(-1);
        
  }
  • Page A
import eventBus from "eventBus.js";
//mounted
eventBus.$on("choiceAddress", function(data){
           console.log(data);//data
           this.getAddress=data;
}.bind(this));
console.log("this.getAddress"+this.getAddress)
//this.getAddress

you can get the parameters passed from page B in page A, but the assignment to this.getAddress does not seem to be assigned outside the eventBus.$on function, and it is still the original value,

.

for example:
I want to call a method when mounted, in which console.log (this.getAddress); will be the original value

mounted(){
    this.a();
},
methods:{
   a(){
    console.log(this.getAddress);
    }
}

the same is true for calling methods not in mounted
how can this value be assigned successfully?

Sep.22,2021

landlord, try the method of obtaining data in page A, write it in created , and do not put it in mounted


clipboard.png
you print out the this here to see if it is an instance of vue here

.
Menu