首页 > 解决方案 > 如何为我的 Vue 应用程序修复“未处理的承诺拒绝”?

问题描述

对于我的 Vue 应用程序,我在 IE 中遇到以下问题:'Unhandled Promise Rejection undefined'

我将问题追溯到应用程序的身份验证。但我不确定如何解决它。我在 next() 函数周围尝试了 try-catch 块。

下面是验证码。

import router from '@/config/Router';
import CONST from '@/utils/Constants';
import ObjectHelper from "@/helpers/ObjectHelper";

class Auth {
    constructor() {
      router.beforeEach((to, from, next) => {
          if (to.matched.some(record => record.meta.requiresAuth)) {

            if (!Auth.isLoggedIn()) {
              next({path: CONST.ROUTE.SESSION.LOGIN});
            } else {
              next();
            }
          } else {
              next();
          }
      });
    }

    static logOut() {
      localStorage.clear();
      sessionStorage.clear();

      router.replace(CONST.ROUTE.SESSION.LOGIN);
    }

    static isLoggedIn() {
      return ObjectHelper.exists(localStorage.getItem(CONST.KEY.AUTH.ACCESS_TOKEN));
    }
}

export default Auth;

this.$router.push(CONST.ROUTE.ORGANISATIONS.OVERVIEW);我的登录组件中,错误被抛出。

为了完整起见,我还提供了路由器的代码:

import Vue from 'vue';
import Router from 'vue-router';

Vue.use(Router);

export default new Router({
  base: process.env.BASE_URL,
  mode: 'history',
  routes: [
    {
      path: '/',
      name: 'home',
      component: () => import('@/views/home/home.vue')
    },{
      path: '/session',
      name: 'session',
      component: () => import('@/views/session/session.vue'),
      children: [
        {
          path: 'login',
          name: 'login',
          component: () => import('@/views/session/login/login.vue')
        },
        {
          path: 'logout',
          name: 'logout',
          component: () => import('@/views/session/logout/logout.vue')
        }
      ]
    },
    {
      path: '/organisations',
      alias: '/organisaties',
      component: () => import('@/views/organisations/organisations.vue'),
      children: [
        {
          path: '',
          name: 'organisations-overview',
          component: () => import('@/views/organisations/overview/overview.vue'),
          meta: {
            requiresAuth: true,
            can: 'edit-organisations',
            fail: '/session/logout'
          }
        },
        {
          path: ':uuid',
          name: 'organisation-edit',
          component: () => import('@/views/organisations/edit/edit.vue'),
          meta: {
            requiresAuth: true,
            can: 'edit-organisations',
            fail: '/session/logout'
          }
        }
      ]
    }
  ]
});

有人知道如何解决这个问题吗?非常感谢!

标签: vue.jsinternet-explorererror-handlinges6-promise

解决方案


我在 IE 中找到了“未处理的承诺拒绝未定义”的解决方案。我需要在我的 Vue 项目中添加对 IE11 的支持:'babel-polyfill'、'weakmap-polyfill'、'core-js/es/set'、'core-js/es/map'。在此之后,一切正常。


推荐阅读