Axios 方法中怎么修改组件的值?

我有一个axios封装的方法:

this.$http.post(Urls.register(), this.formItem).then(function (response) {
          
        }.bind(this))
          .catch(function (response) {
            // 这里我想改变 this.$data.loading=false 怎么做?
            console.log(response)
          })

我在.catch()方法中想要改变一个组件的值怎么改变?这里好像不能使用this.loading,这里的thisundefined

阅读 3.9k
2 个回答
this.$http.post(Urls.register(), this.formItem).then(response=>{
          
        })
          .catch(response=>{
            this指向vue
            console.log(response)
          })



使用箭头函数,更改this指向Vue,或者你继续在catch后面bind(this),但是es6的箭头函数已经很普及了

this.$http.post(Urls.register(), this.formItem).then(function (response) {
          
        }.bind(this))
          .catch((response) => {
            this.$data.loading=false
            console.log(response)
          })
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题