vue事件監(jiān)聽函數(shù)on中的this指針域使用
事件監(jiān)聽函數(shù)on中this指針域
使用eventBus在兩個組件A,B之間通訊:
//定義全局eventBus用于事件傳遞
var bus = new Vue();
//A組件
var A = Vue.component({
?? ?...
?? ?
?? ?data:{
?? ??? ?dataA:1,
?? ?},
?? ?//在鉤子函數(shù)中將監(jiān)聽_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)聽事件_event事件的函數(shù)前需要提前保存this指針為this_com,因為在監(jiān)聽函數(shù)中的this不再指向A組件本身,而是指向事件監(jiān)聽者bus。
另一種方案是用箭頭函數(shù)代替事件監(jiān)聽函數(shù),因為箭頭函數(shù)可以將指針域綁定到當(dāng)前組件
var A = Vue.component({
?? ?...
?? ?
?? ?data:{
?? ??? ?dataA:1,
?? ?},
?? ?//在鉤子函數(shù)中將監(jiān)聽_event事件
?? ?created:function(){
?? ??? ?var this_com = this;
?? ??? ?bus.$on('_event', value=>{
?? ??? ??? ?this_com.dataA = value;
?? ??? ?})
?? ?},
?? ?
?? ?...
})vue中的this問題
在vue中當(dāng)在vue中使用匿名函數(shù)的時候,會出現(xiàn)this指針改變的問題,出現(xiàn)方法或者屬性數(shù)據(jù)undefine的問題,以下是相關(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ù)中有時候回調(diào)函數(shù)會修改this的指向,this本來應(yīng)該指的是vue這個對象,但是他的this指向的是回調(diào),由于在回調(diào)函數(shù)改變了this指針,導(dǎo)致后序出現(xiàn)this指向的數(shù)據(jù)出現(xiàn)未定義的狀況。
但是在箭頭函數(shù)中this指向的永遠(yuǎn)都是vue對象,所以建議多是想用箭頭函數(shù)。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue項目中實現(xiàn)el-dialog組件可拖拽效果
本文主要介紹了vue項目中實現(xiàn)el-dialog組件可拖拽效果,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01
解決vue.js中settimeout遇到的問題(時間參數(shù)短效果不穩(wěn)定)
這篇文章主要介紹了解決vue.js中settimeout遇到的問題(時間參數(shù)短效果不穩(wěn)定),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07
Vue 將后臺傳過來的帶html字段的字符串轉(zhuǎn)換為 HTML
這篇文章主要介紹了Vue 將后臺傳過來的帶html字段的字符串轉(zhuǎn)換為 HTML ,需要的朋友可以參考下2018-03-03
Vue中實現(xiàn)父子組件雙向數(shù)據(jù)流的三種方案分享
通常情況下,父子組件的通信都是單向的,或父組件使用props向子組件傳遞數(shù)據(jù),或子組件使用emit函數(shù)向父組件傳遞數(shù)據(jù),本文將嘗試講解Vue中常用的幾種雙向數(shù)據(jù)流的使用,需要的朋友可以參考下2023-08-08

