鍍金池/ 問答/HTML/ axios 回調(diào)方法失敗,但是把方法寫回調(diào)里是成功的

axios 回調(diào)方法失敗,但是把方法寫回調(diào)里是成功的

export default {

mounted() {
let para = {
  offset: 0,
  limit: 50000,
  exchangeNo: ""
};
var _this = this;
let commodityCode = [];
axios.post('/api/zd/futures/list', para)
  .then(function(response) {
    _this.total = response.data.total;
    for (var i = 0; i < response.data.rows.length; i++) {
      commodityCode.push(response.data.rows[i].code)
    }
   _this.initlocalStore(_this.commodityCode)//方法中初始化了localstore,把一個參數(shù)寫到localstore里了

    _this.websocketsend(`shzd@1@${_this.storage.commodityCodeList}`) //websocket發(fā)送請求 this.websock.send(agentData);  
  })
  .catch(function(error) {
    console.log(error);
  });

圖片描述

回答
編輯回答
深記你

作用域問題,this指向錯誤,不是你認(rèn)為的vm,而是指向了window,所以找不到initlocalStore

可以使用你上面定義的_this或箭頭函數(shù)

2018年5月1日 12:05