import axios from "axios";
export default {
data() {
return {
obj: {},
};
},
created() {
this.start();
this.end();
},
methods: {
findList(method, url) {
return axios({
method: method,
url: url,
});
},
start() {
var that = this;
this.findList("get", "xxxxxxxxx").then(
function (res) {
var aaa = JSON.parse(res.data.monitdatas);
that.obj = aaa;
console.log(aaa);
}
);
},
end() {
console.log(this.obj);
},
},
};
你现在写的就没问题 。 因为 接口需要 时间 是异步 的 先 运行了end再 运行的atart 。 如果想 end和start输出一样 。在dtart 运行end.
function (res) {
var aaa = JSON.parse(res.data.monitdatas);
that.obj = aaa;
this.end();
console.log(aaa);
}
是 this.obj = aaa;
这个应该是因为前端代码顺序执行导致start未获得返回结果end先执行了,axios的话可以试一下设置 async: false 同步请求会等待后台请求响应后再往下执行
就是你这样去接收,之所以end函数中的输出为空时因为请求接口是异步 你把this.end();这个调用放到上面的.then中的函数中就行了