鍍金池/ 問(wèn)答/HTML5  HTML/ vue 兄弟組件傳值this指向問(wèn)題

vue 兄弟組件傳值this指向問(wèn)題

vue兄弟組件傳值,接收值的時(shí)候,this指向有問(wèn)題?

a.vue

import Bus from '../../utils/bus.js' //公共vue 傳值

export default {
    components: {
        baidumap,
        bmView
    },
    data () {
        return {
            zoom: 10,
            address: '點(diǎn)我選擇'
        }
    },
    mounted () {
        this.createdMap();

        let that = this;

        Bus.$on('addValue',function (argument) {
            console.log(that)

            if (argument) {
                that.address = argument.ci_name
                console.log(that)
            }
        })
        console.log(this)
        console.log(that)
    }
}

b.vue

import Bus from '../../utils/bus.js'
export default {
    data () {
        return {
            proshow:true,
            cityshow:false,
            countyshow:false,
            townshow:false,
            proArr: [],
            cityArr: [],
            countyArr: [],
            townArr: []
        }
    },
    methods: {
        selectpro (item) {
            if (item.ci_id == 36) {
                Bus.$emit('addValue',item)
                this.$router.go(-1);
                return;
            }
        }
    }
}

在a.vue中輸出的this的指向?yàn)?br>![![圖片描述][2]][1]

圖片描述

bus.js中的 新vue實(shí)例
圖片描述

為何他們的_uid不一樣 而且在_uid為3的實(shí)例中address數(shù)據(jù)是已經(jīng)更改的,在_uid為50的實(shí)例中address的數(shù)據(jù)是沒有更改的?

回答
編輯回答
涼薄

前兩個(gè)和后兩個(gè)不是一起執(zhí)行輸出的吧
我覺得你應(yīng)該是
Bus.$emit('addValue',item)的時(shí)候觸發(fā)了之前注冊(cè)過(guò)的Bus.$on

this.$router.go(-1)的時(shí)候觸發(fā)了a.vue的mounted

2017年4月29日 02:07
編輯回答
膽怯

差不多是這樣

var obj = {
  name:"123",
  show:function(){
    console.log(this);
  }
}

obj.show();//{name:"123"}
obj.name = 'abc';
obj.show();//{name:"abc"}

路由跳轉(zhuǎn)導(dǎo)致vue實(shí)例更新

2017年8月7日 22:38