首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Vue Router -访问Vue实例,而不调用next()

Vue Router -访问Vue实例,而不调用next()
EN

Stack Overflow用户
提问于 2020-03-18 19:27:59
回答 1查看 55关注 0票数 0

例如,如果验证失败,我正在设法将我的SPA推送到某个路由。我使用beforeRouteEnterbeforeRouteUpdate方法来运行一个函数来确定这一点;

代码语言:javascript
复制
...
beforeRouteEnter(to, from, next) {
    loading(next);
},
beforeRouteUpdate(to, from, next) {
    loading(next);
},
...

然后验证这个函数(这只是暂时的,直到我弄清楚这一点)。

代码语言:javascript
复制
let loading = function(callback) {
    db.commit('setPageLoading', true);
    new requests().concat([
        'UserController@userDetails',
        'SettingController@settings',
        'ClientController@allClients',
        'StatsController@statsUsage',
        'UserController@paymentDetails'],
    () => {
        db.commit('setPageLoading', false);
        /** everything's all gravy, `next()` should be called now! */
        callback();
    },
    (error) => {
        /**
        * I dont want to call `next()` to access the vue instance as it will show the requested page
        * but I have to to access the router!!!
        **/
        callback(vm => {
            switch(error.response.status) {
                case 401:
                    /** Put login box here, this will do for now **/
                    vm.$router.push('/login').catch(err => {});
                break;
                /** Custom error to tell the SPA the user needs to fill in their company details first */
                case 498:
                    for(let i in error.response.data.errors) {
                        db.commit('addErrorNotification', error.response.data.errors[i][0]);
                    }
                    vm.$router.push('/profile').catch(err => {});
                break;
            }
        });
    });
};

我的问题是,我必须调用next()来访问vue实例以推送到不同的路由。但是我调用next()回调,它告诉Vue Router继续加载当前或挂起的路由,这不是我想要发生的事情。

有没有人能告诉我一种不用调用next()回调就能访问Vue实例的方法?

问候

EN

回答 1

Stack Overflow用户

发布于 2020-03-18 19:45:58

正如Delena Malan所描述的,谢谢!

代码语言:javascript
复制
let loading = function(callback) {
    db.commit('setPageLoading', true);
    new requests().concat([
        'UserController@userDetails',
        'SettingController@settings',
        'ClientController@allClients',
        'StatsController@statsUsage',
        'UserController@paymentDetails'],
    () => {
        db.commit('setPageLoading', false);
        /** everything's all gravy, `next()` should be called now! */
        callback();
    },
    (error) => {
        switch(error.response.status) {
            case 401:
                /** Put login box here, this will do for now **/
                callback({ path: '/login' });
            break;
            case 498:
                for(let i in error.response.data.errors) {
                    db.commit('setNotifications', []);
                    db.commit('addErrorNotification', error.response.data.errors[i][0]);
                }
                callback({ path: '/profile' });
            break;
        }
        db.commit('setPageLoading', false);
    });
};
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60738565

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档