JS等待在浏览器控制台中引发错误

时间:2019-12-15 11:35:36

标签: javascript laravel vue.js async-await nuxt

我正在使用nuxt为laravel项目开发客户端。

在login.vue组件中,我具有以下JS代码

import Form from 'vform'

export default {
  head () {
    return { title: this.$t('login') }
  },

  data: () => ({
    form: new Form({
      email: '',
      password: ''
    }),
    remember: false
  }),

  methods: {
    async login () {
      let data;

      // Submit the form.
      try {
        const response = await this.form.post('/api/login');
        data = response.data;
      } catch (e) {
        return;
      }

      // Save the token.
      this.$store.dispatch('auth/saveToken', {
        token: data.token,
        remember: this.remember
      });

      // Fetch the user.
      await this.$store.dispatch('auth/fetchUser');

      // Redirect home.
      this.$router.push({ name: 'home' })
    }
  }
}

如果我尝试使用错误的电子邮件和密码值提交登录表单,则会在浏览器控制台中看到错误消息。

例如:

POST http://laravel.local/api/login 422 (Unprocessable Entity)

请注意,我正在使用try catch来捕获以下调用中的所有错误。

const response = await this.form.post('/api/login');

这真的与异步/等待使用有关吗? 如何摆脱浏览器控制台中的错误?

如果您需要我的更多信息,请随时询问。

0 个答案:

没有答案
相关问题