场景
- 在使用
axios
与服务器进行通信时,回调函数中无法将response
中的数据传递给vue
,如下所示:
调试时const app = Vue.createApp({ data() { return { }; }, mounted() { }, methods: { callGm() { console.log(this.curGm); axios .get("api/gm", { params: this.curGm.vals }) .then(function (response) { this.curGm = response.data; }); }, }, }); const vm = app.mount("#app");
this
为windows
解决方式
- 使用
lambda
表达式callGm() { console.log(this.curGm); axios .get(window.location.origin +"/api/gm", { params: this.curGm.vals }) .then((response) => { this.curGm = response.data; }); },