JS -- ajax中 this 和 _self 的使用

reject(){
   	let _self = this;
	var _appInstArr =[];
	_appInstArr.push(_self.appInstId);
	$.ajax({
		url : path + '/appManagement/appManagementAction!updateAppInstancesStatusByIds.action',
		cache: false,
		type : 'post',
		data : {
			appInstIds :  _appInstArr.toString(),
			expectStatus: 9
		},
		success : function(data) {
			if(data == "success"){
				_self.$router.push({path:"/proxyList"});
				top.navigatorTop.removeLastBreadNode(false);
				_self.$message({
			         showClose: true,
			         message: _self.$t('tips.rejectSuccess'),
			         type: 'success'
			    });
				return;
			}
			else{
				_self.$message({
					showClose: true,
					message: _self.$t('tips.rejectFailed'),
					type: 'error'
				});
			}
		},
		error : function(err) {
			_self.$message({
				showClose: true,
				message: _self.$t('tips.rejectFailed'),
				type: 'error'
			});
		}
	});
},

在 ajax 中,不要隨便用 this,而要用 _self

如果在 success 中,將 _self.$router.push({path:"/proxyList"}); 變更爲 this.$router.push({path:"/proxyList"}); ,將會報錯:

Uncaught TypeError: Cannot read property 'push' of undefined
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章