简体   繁体   English

如何修复 VueJS 中的“这是未定义的”?

[英]How can i fix “this is undefined” in VueJS?

The line this.$store.commit('disconnect');this.$store.commit('disconnect'); is causing a "this is undefined" error, what can i do?导致“这是未定义”错误,我该怎么办?

store/index.js:存储/index.js:

export const state = () => ({
  log: false,
  user: {token: null, id: null, username: null, email: null, created_at: null, pseudo: null}
})


export const mutations = {
  connect (state) {
    state.log = true
  },
  disconnect (state) {
    state.log = false,
    state.user.id = null,
    state.user.username = null,
    state.user.email = null,
    state.user.created_at = null,
    state.user.pseudo = null
  }
}

index.vue索引.vue

<script>
import axios from "axios";

  methods: {
    async login() {
    var self = this;

    const auth_res = axios({
      method:'post',
      url:'http://127.0.0.1:8000/v2.0/auth',
      data:{
        username: this.username,
        password: this.password
      }
      }).then(function(res){
        this.$store.commit('disconnect');
        self.$router.push('/');
      }).catch(function (erreur) {
        console.log(erreur);
      });
    }
  }
}
</script>

If i delete the line of the commit in the index.vue file, it works fine, but i need to use it so how can i fix this?如果我删除index.vue文件中的commit行,它工作正常,但我需要使用它,那么我该如何解决这个问题?

Thanks again再次感谢

In the callback context, this tense to callback itself.在回调上下文中, this时态回调本身。 That's why you are getting the error.这就是您收到错误的原因。 You should replace it with,您应该将其替换为,

self.$store.commit('disconnect');

where the self holds the actual context of Vue. self持有 Vue 的实际上下文。 Here is the explanation,这里是解释,

methods: {
    async login() {
    var self = this; // You got the context of the method which belongs to Vue

    const auth_res = axios({
      method:'post',
      ...
      }).then(function(res){
        self.$store.commit('disconnect'); // The self.$store is now avaialable in callback
        self.$router.push('/');
      }).catch(function (erreur) {
        ...
      });
    }
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在VueJS中修复“这是未定义的”? - How to fix “this is undefined” in VueJS? 我如何做一个设置器来解决计算的属性问题 - VueJS - How can i do a setter to fix computed propery problem - VueJS 如何修复“TypeError: undefined is not an object”? - How can I fix 'TypeError: undefined is not an object'? 如何修复无法读取未定义的属性“错误” - how can I fix Cannot read property 'error' of undefined 我的 Function 在爆发前只循环一次。 我怎样才能解决这个问题? Vuejs - My Function is only looping once before it breaks out. How can i fix this? Vuejs 尝试在 vuejs 中更新 firebase 文档时,我无法解决此未定义字段问题 - I cannot fix this undefined field issue when trying to update firebase document in vuejs Vuejs1:如何在PHP中编码Vuejs参数字符串 - Vuejs1: How can I encode a Vuejs argument string in PHP 整数到字符串,我该如何解决以下错误:“TypeError: Can&#39;t convert undefined to object”? - Integer to String, How can I fix the following error: "TypeError: Can't convert undefined to object"? 无法读取未定义的属性“createMessageCollector”,正在工作,但我什么都没做,现在它坏了,我该如何修复? - Cannot read property 'createMessageCollector' of undefined, was working and i changed nothing and now its broken, how can i fix? 由于脚本加载问题,当我重新加载页面时谷歌地图未定义,我该如何解决这个问题? - Google Maps is undefined when I reload the page due to script loading issues, how can I fix this?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM