Error in v-on handler: “TypeError: this is not a function”

在进行简单登录跳转的测试中,出现Error in v-on handler: "TypeError: this is not a function"的错误

async UserLogin() {
      try {
        //登录成功
        const {phone, password} = this
        phone && password && await this.$store.dispatch('UserLogin', {phone, password})
        //跳转到home首页
        this.$router.push('/home');
      } catch (error) {
        alert(error.message);
      }
    }

原因:

在 const {phone, password} = this 代码后面没有加上分号,导致代码解析的时候误认为该行代码没有结束,this指向不一致


解决方案:

只需要在后面加上分号即可

async UserLogin() {
      try {
        //登录成功
        const {phone, password} = this;
        phone && password && await this.$store.dispatch('UserLogin', {phone, password});
        //跳转到home首页
        this.$router.push('/home');
      } catch (error) {
        alert(error.message);
      }
    }
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐