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

挑战
  • 599
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绑定给这个回调函数

回复
阅读 1.6k
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,
          });
        }
});
宣传栏