vue事件監(jiān)聽(tīng)函數(shù)on中的this指針域使用
事件監(jiān)聽(tīng)函數(shù)on中this指針域
使用eventBus在兩個(gè)組件A,B之間通訊:
//定義全局eventBus用于事件傳遞
var bus = new Vue();
//A組件
var A = Vue.component({
?? ?...
?? ?
?? ?data:{
?? ??? ?dataA:1,
?? ?},
?? ?//在鉤子函數(shù)中將監(jiān)聽(tīng)_event事件
?? ?created:function(){
?? ??? ?var this_com = this;
?? ??? ?bus.$on('_event', function(value){
?? ??? ??? ?this_com.dataA = value;
?? ??? ?})
?? ?},
?? ?
?? ?...
})
//B組件
var B = Vue.component({
?? ?...
?? ?data:{
?? ??? ?dataB = 2;
?? ?},
?? ?methods:{
?? ??? ?changeA:function(){
?? ??? ??? ?//觸發(fā)事件_event
?? ??? ??? ?bus.$emit('_event', this.dataB);
?? ??? ?},
?? ?},
?? ?template:`
?? ??? ?<div v-bind:click="this.changeA"></div>
?? ?`
})可以看到,在組件A監(jiān)聽(tīng)事件_event事件的函數(shù)前需要提前保存this指針為this_com,因?yàn)樵诒O(jiān)聽(tīng)函數(shù)中的this不再指向A組件本身,而是指向事件監(jiān)聽(tīng)者bus。
另一種方案是用箭頭函數(shù)代替事件監(jiān)聽(tīng)函數(shù),因?yàn)榧^函數(shù)可以將指針域綁定到當(dāng)前組件
var A = Vue.component({
?? ?...
?? ?
?? ?data:{
?? ??? ?dataA:1,
?? ?},
?? ?//在鉤子函數(shù)中將監(jiān)聽(tīng)_event事件
?? ?created:function(){
?? ??? ?var this_com = this;
?? ??? ?bus.$on('_event', value=>{
?? ??? ??? ?this_com.dataA = value;
?? ??? ?})
?? ?},
?? ?
?? ?...
})vue中的this問(wèn)題
在vue中當(dāng)在vue中使用匿名函數(shù)的時(shí)候,會(huì)出現(xiàn)this指針改變的問(wèn)題,出現(xiàn)方法或者屬性數(shù)據(jù)undefine的問(wèn)題,以下是相關(guān)的解決方法
在回調(diào)函數(shù)之前重新將this賦值
例如
?connection() {
? ? ? // 更換that指針
? ? ? var that = this
? ? ? const socket = new SockJS('http://localhost:8080/test-info')
? ? ? this.stompClient = Stomp.over(socket)
? ? ? console.log('----------------')
? ? ? console.log(this.stompClient)
? ? ? console.log('----------------')
? ? ? const tt = this.stompClient
? ? ? // 此處不能使用 this.stompClient
? ? ? tt.connect({}, function(frame) {
? ? ? ? console.log('++++++++++++++++')
? ? ? ? console.log('Connected: ' + frame)
? ? ? ? console.log('++++++++++++++++')
?
? ? ? ? tt.subscribe('/stock/price', function(val) {
? ? ? ? ? console.log(val)
? ? ? ? ? console.log(JSON.parse(val.body))
? ? ? ? ? that.list1 = JSON.parse(val.body)
? ? ? ? })
? ? ? })
? ? }使用箭頭函數(shù)
? connection() {
? ? ? // 更換that指針
? ? ? const socket = new SockJS('http://localhost:8080/test-info')
? ? ? this.stompClient = Stomp.over(socket)
? ? ? console.log('----------------')
? ? ? console.log(this.stompClient)
? ? ? console.log('----------------')
? ? ? ?this.stompClient.connect({}, (frame) => {
? ? ? ? console.log(frame)
? ? ? ? this.stompClient.subscribe('/stock/price', (val) => {
? ? ? ? ? console.log('--------list1-----------')
? ? ? ? ? console.log(this.list1)
? ? ? ? ? console.log('--------list1-----------')
? ? ? ? ? this.list1 = JSON.parse(val.body)
? ? ? ? })
? ? ? })
? ? }在回調(diào)函數(shù)中有時(shí)候回調(diào)函數(shù)會(huì)修改this的指向,this本來(lái)應(yīng)該指的是vue這個(gè)對(duì)象,但是他的this指向的是回調(diào),由于在回調(diào)函數(shù)改變了this指針,導(dǎo)致后序出現(xiàn)this指向的數(shù)據(jù)出現(xiàn)未定義的狀況。
但是在箭頭函數(shù)中this指向的永遠(yuǎn)都是vue對(duì)象,所以建議多是想用箭頭函數(shù)。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue項(xiàng)目中實(shí)現(xiàn)el-dialog組件可拖拽效果
本文主要介紹了vue項(xiàng)目中實(shí)現(xiàn)el-dialog組件可拖拽效果,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
解決vue.js中settimeout遇到的問(wèn)題(時(shí)間參數(shù)短效果不穩(wěn)定)
這篇文章主要介紹了解決vue.js中settimeout遇到的問(wèn)題(時(shí)間參數(shù)短效果不穩(wěn)定),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07
Vue 將后臺(tái)傳過(guò)來(lái)的帶html字段的字符串轉(zhuǎn)換為 HTML
這篇文章主要介紹了Vue 將后臺(tái)傳過(guò)來(lái)的帶html字段的字符串轉(zhuǎn)換為 HTML ,需要的朋友可以參考下2018-03-03
Vue中實(shí)現(xiàn)父子組件雙向數(shù)據(jù)流的三種方案分享
通常情況下,父子組件的通信都是單向的,或父組件使用props向子組件傳遞數(shù)據(jù),或子組件使用emit函數(shù)向父組件傳遞數(shù)據(jù),本文將嘗試講解Vue中常用的幾種雙向數(shù)據(jù)流的使用,需要的朋友可以參考下2023-08-08
Vue中代碼傳送(teleport)的實(shí)現(xiàn)
本文主要介紹了Vue中代碼傳送(teleport)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04

