【问题标题】:Make nested route inside AuthenticatedRouteMixin accessible使 AuthenticatedRouteMixin 内的嵌套路由可访问
【发布时间】:2018-11-20 01:00:04
【问题描述】:

我想“释放”一个嵌套路由,这样即使没有登录的用户也可以访问该路由。

例如:

posts - /create - /edit - /show

posts 路线上,我使用了AuthenticatedRouteMixin。 有了这个,所有的子路由都被自动保护了。现在我只想让/show 可访问。我知道我可以在 /create/edit 上使用 mixin 并将其从 posts 路由中删除,但是如果您有 10 多个嵌套路由并且其中只有 1 个也应该可用于未登录的用户,那就太好了不方便。

您知道应对这一挑战的其他解决方案吗?

如果没有,我想我必须为此编写一个额外的 mixin...

谢谢!

【问题讨论】:

    标签: ember.js ember-simple-auth


    【解决方案1】:

    ember-simple-auth 的 AuthenticatedRouteMixin 使用 beforeModel 钩子来检查 session.isAuthenticated 与否。您需要覆盖“显示”路由中的 beforeModel 以通过一起绕过 AuthenticatedRouteMixin 的 super() 实现来跳过 Auth 检查。

    beforeModel (transition, skipAuthCheck) {
        if (!skipAuthCheck) {
           return this._super(...arguments, true);
        }
    }
    

    检查'show' beforeModel 是否与父路由有依赖关系。即'posts',在父路由上执行此检查。

    【讨论】:

    • 我会在接下来的几天里检查一下。现在谢谢!
    【解决方案2】:

    您可以使用 path 参数伪造嵌套路由:

    this.route('posts', function() {
        this.route('create');
        this.route('edit');
    });
    this.route('posts-show', { path: '/posts/show' });
    

    【讨论】:

    • 我绝对想避免这种情况,因为我想将我的文件按语义组织在posts 文件夹下。还是感谢您的回复!
    猜你喜欢
    • 2020-07-18
    • 2017-10-02
    • 1970-01-01
    • 1970-01-01
    • 2016-10-07
    • 1970-01-01
    • 1970-01-01
    • 2019-01-06
    • 1970-01-01
    相关资源
    最近更新 更多