简体   繁体   English

当路由不存在时,铁路由器渲染notFound模板

[英]iron-router render notFound template when route does not exist

If the data function returns a falsy value like null, the NotFound template will be rendered in my application. 如果数据函数返回虚假的值(如null),则NotFound模板将在我的应用程序中呈现。 This works fine, but now I want also to render the NotFound template, if the route does not exist. 这可以正常工作,但是如果路由不存在,我现在还想渲染NotFound模板。

For instance: 例如:

this.route('settingsOverviewPage', {
        path: '/settings',
        data: function() { return Users.findOne(Meteor.userId()); },
        waitOn: function() {
            if (Meteor.userId()) {
                return Meteor.subscribe('ownUser', Meteor.userId());
            }
            return null;
        }
    });

If I use this route: /settings12345 for instance, the browser reloads, but it renders the last route. 如果我使用以下路线:例如, /settings12345 ,浏览器将重新加载,但会呈现最后一条路线。

Any help would be greatly appreciated. 任何帮助将不胜感激。

You have to define a "catch-all" route like this : 您必须定义一个“包罗万象”的路由,如下所示:

this.route("notFound",{
  path:"*",
  template:"notFoundTemplate"
});

It is important that you define this route as the LAST one, otherwise it will catch valid URLs. 将此路由定义为LAST,这一点很重要,否则它将捕获有效的URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM