this.$store这里的this指向不正确

const doLogin = function (username, password, v, callback) {
};
doLogin(localStorage.username, localStorage.password, this, function (res, msg) {
        if (res) {
          this.$store.commit({
            type: 'login',
            username: res.name,
            token: res.token,
          });
      });

this.$store这里的this指向不正确怎么把外层this绑定给这个回调函数

阅读 2.2k
1 个回答

使用箭头函数

const doLogin = function (username, password, v, callback) {
};
doLogin(localStorage.username, localStorage.password, this, (res, msg) => {
        if (res) {
          this.$store.commit({
            type: 'login',
            username: res.name,
            token: res.token,
          });
        }
});
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题