gpt4 book ai didi

javascript - 对 meteor 铁路由器中的主路线做一些不同的事情

转载 作者:行者123 更新时间:2023-11-27 23:49:55 25 4
gpt4 key购买 nike

我在我的 meteor 应用程序中使用 Iron-Route 进行路由。由于我有多个模块并且我想避免多次编码,因此我将用于身份验证( Angular 色)的过滤器放入核心包中:

核心包

var filters = {
authenticate: function () {
var user;
if (Meteor.loggingIn()) {
this.layout('login');
this.render('loading');
} else {
user = Meteor.user();
if (!user) {
this.layout('login');
this.render('signin');
return;
}
this.layout('StandardLayout');
this.next();
}
}
}
Router.configure({
layoutTemplate: 'layout',
loadingTemplate: 'loading',
notFoundTemplate: 'notFound',
before: filters.authenticate
});
Router.route('/', function () {
this.render('contentTemplate', { to: 'content' });
this.render('navigationTemplate', { to: 'navigation' },)
});

其他软件包

...只需要这个:

Router.route('/article/:_id', {
name: 'article',
yieldTemplates: {
'navigationPage': { to: 'navigation' },
'contentPage': { to: 'content' }
}
});

现在在显示任何内容之前都会检查每个模块(路由)。但我需要首页异常(exception)。未登录的用户也应该可以访问主页 Route.route('/')。我该怎么做?

最佳答案

您可以使用 iron:router 中的 onBeforeAction Hook ,如下所示。此解决方案假设您的登录路由名为 'login' 。但可以修改以满足您的需求。

./client/lib/hoooks/router.js

Router.onBeforeAction(function () {
// all properties available in the route function
// are also available here such as this.params
if (!Meteor.userId()) {
//code for action to be taken when user is not logged in
this.redirect('login');
this.stop();
} else {
this.next();
}
},{except: ['homeRouteName'] });

注意:将“homeRouteName”替换为 '/' 处的路线名称

关于javascript - 对 meteor 铁路由器中的主路线做一些不同的事情,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32808554/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com