使用axios请求数据,在.then()里面是可以使用的,但是在外面就没了
修改后
不是this指向的问题,请求是异步的,先打印出 然后在执行axios请求,
this指向不正确,应该这样写:
this
getData(){ let _this = this; _this.$http.post(...).then(res => { _this.DataAll = res.data.data; console.log(_this.DataAll); }); console.log(_this.DataAll); }
异步
外面声明 var that = this
this 指向问题你里面的this 指向是本身
是异步请求啊,先执行的最下面的那个console,这时DataAll数据为空,异步请求到数据后才填充进DataAll的
这里肯定不是this指向问题啊,axios请求是异步的,楼上的说的对