久久福利_99r_国产日韩在线视频_直接看av的网站_中文欧美日韩_久久一

您的位置:首頁技術(shù)文章
文章詳情頁

在vue中使用回調(diào)函數(shù),this調(diào)用無效的解決

瀏覽:108日期:2022-12-09 18:24:43

let self = this //使用新變量替換this,以免this無效

//updateStudentInfoToServer是一個將本身部分?jǐn)?shù)據(jù)異步上傳的接口,接收三個參數(shù),其中第一個是數(shù)據(jù),第二、三個是函數(shù),第二、三個函數(shù)使用function(){}形式書寫

updateStudentInfoToServer:function(data, networkOk, networkError){ let postData = this.$qs.stringify({ data:data }) this.axios.post(’/api/update/updateStudentInfo’, postData ).then(res=>{ console.log(’ return : ’) console.log(res) networkOk(res) //網(wǎng)絡(luò)成功的回調(diào) }).catch(error=>{ console.log(error) networkError(error) //網(wǎng)絡(luò)失敗的回調(diào) }) console.log(’axios done’)}, this.updateStudentInfoToServer(data, function(res){ console.log(’return ok’) console.log(res) // console.log(’self’) // console.log(self) //就是this // console.log(’this’) // console.log(this) //undefined self.handleCancelEdit() },function(error){ console.log(error) } )

提交網(wǎng)絡(luò)數(shù)據(jù)是異步調(diào)用,所以會先返回然后才執(zhí)行成功、失敗的回調(diào)。

這種書寫方式,function的作用于決定了function的代碼塊內(nèi)使用this無法改變、獲取vue data中設(shè)置的變量

使用es6的箭頭語法可以實(shí)現(xiàn)this的隨處調(diào)用

this.updateStudentInfoToServer(this, res=>{ console.log(’return ok’) console.log(res) console.log(’self’) console.log(self) console.log(’this’) console.log(this)//this和self一樣 }, error=>{ console.log(error) })

不過某些瀏覽器的某些版本不支持es6的語法,可能導(dǎo)致各種各樣的問題

補(bǔ)充知識:vue在全局函數(shù)中加回調(diào),調(diào)用vue文件中的函數(shù)

全局函數(shù)可以寫一個文件globalFunc.js

exports.install = function(Vue, option){ Vue.prototype.setData = function(that, key){ that[key] = ’222’ } Vue.prototype.testCallMe = function(str){ console.log(’test call me’ + str) } Vue.prototype.testCallBack = function(func, param){ func(param) this.testCallMe(’tetetet’) }}

main.js

import globalFunc from ’@/components/globalFunc’

Vue.use(globalFunc)

vue文件中

調(diào)用

this.testCallBack(this.test, ’yui0’)//使用全局函數(shù)調(diào)用vue文件中的函數(shù),修改vue文件中的數(shù)據(jù)

this.setData(this, ’msg’)//使用全局函數(shù)修改vue文件中的數(shù)據(jù)

test函數(shù)編寫

test:function(str){ this.msg = ’233’ + str},

以上這篇在vue中使用回調(diào)函數(shù),this調(diào)用無效的解決就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Vue
主站蜘蛛池模板: 日本成人小视频 | 国产成人精品午夜视频' | 欧美日韩二区三区 | 一区二区精品 | 亚洲免费视频一区二区 | 国产一区在线免费观看 | 欧美综合久久 | 一区二区精品在线 | 日韩免费高清视频 | 日韩精品免费视频 | 久久久久99 | 91婷婷射 | 自拍偷拍99 | 日韩中文在线 | 亚洲激情视频在线播放 | 97超碰在线免费 | 国产精品成人av | 久久一级 | 日本在线一区二区三区 | 亚洲在线免费观看 | 免费av中国 | 男女免费视频 | 嫩草影院黄色 | 精品乱子伦一区二区三区 | 欧美一区二区三区在线 | 欧美日韩视频网站 | 男女视频在线观看 | 久久久久综合 | 7777奇米影视 | 一级欧美日韩 | 在线观看免费视频日韩 | 91在线观看网站 | 激情五月婷婷综合 | 日韩精品一区二区三区四区五区 | 成人1区2区 | 亚洲麻豆| 亚洲精选免费视频 | 免费国产视频在线观看 | av网站免费观看 | 欧美精品一区自拍a毛片在线视频 | 国产精品免费在线 |