【错误记录/vue.js】axios中的response数据无法传递给vue

场景

  • 在使用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");
    
    调试时thiswindows
    在这里插入图片描述

解决方式

  • 使用lambda表达式
    callGm() {
          
          
      console.log(this.curGm);
      axios
        .get(window.location.origin +"/api/gm", {
          
           params: this.curGm.vals })
        .then((response) => {
          
          
            this.curGm = response.data;
        });
    },
    
    在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_33446100/article/details/121001600