铁路由器不显示notFound页面

时间:2015-02-07 07:27:45

标签: meteor iron-router

TL,DR:如果路由器仅在客户端运行,如何使未找到的模板在全局布局模板中工作(这是一项要求:客户端和服务器路由是分开的。 )

我需要铁:路由器在无法匹配任何路由器时显示默认模板 在我的应用程序中,所有路由器都是客户端(router.js文件位于客户端文件夹中。)
到目前为止,这就是我的代码:

  1. router.js - 客户端
  2. Router.configure({
        layoutTemplate: 'ApplicationLayout'
    });
    
    Router.map(function() {
        this.route('home', {
            path: '/',
    
            onAfterAction: function() {
                document.title = 'MyApp';
            },
    
            action: function() {
                this.render('Home');
                this.render('Menu', {to: 'menu'});
                this.render('Footer', {to: 'footer'});
            } 
        });
    
        this.route('notFound', {
            path: '*',
    
            onAfterAction: function() {
                document.title = 'Page not found - MyApp';
            },
    
            action: function() {
                this.render('NotFound');
                this.render('Menu', {to: 'menu'});
                this.render('Footer', {to: 'footer'});
            }
    
        });
    });

    1. ApplicationLayout.html
    2. <template name="ApplicationLayout">
            
        <div class="nav-container">
          {{> yield "menu"}}
        </div>
      
        <div class="main-container">
          {{> yield}}
        </div>
      
        <div class="footer-container">
          {{> yield "footer"}}
        </div>
      
      </template>

      http://eventedmind.github.io/iron-router/#404s-and-client-vs-server-routes阅读文档时,服务器似乎发送了404,但事实并非如此。所以我在服务器端创建了另一个发送404的路由器,但即便没有修复。

      1. router.js - 服务器端
      2. Router.map(function() {
            this.route('notFound', {
                path: '*',
        
                action: function() {
                    this.response.statusCode = 404;
                    this.response.end();
                }
            }); 
        });

        到目前为止的结果:
        Oops, looks like there's no route on the client or the server for url: "http://localhost:3000/logi/."

        其他尝试:
        我尝试在全局路由器配置中包含notFoundTemplate,并且它确实显示 NotFound模板,但 不在 {{1}内} 未加载layoutTemplateMenu模板。但是,如果我理解正确,当Footer函数返回data - https://github.com/EventedMind/iron-router/issues/116时会调用此模板,请参阅 cmather 的答案。

        相关详情:

        1. meteor@1.0.3.1
        2. 铁:router@1.0.7
        3. 修改

          使用配置中的null属性确实显示 notFoundTemplate模板,但省略了NotFoundMenu模板。

3 个答案:

答案 0 :(得分:4)

所有你需要的是:

  

在路由器配置(服务器和客户端位置)中定义常规模板

Router.configure({
  layoutTemplate: "MasterLayout",
  loadingTemplate: "Loading",
  notFoundTemplate: "NotFound"
});
  

在客户端文件夹

下定义常规模板
<template name="NotFound">
  Ups... nobody here?
</template>

<template name="Loading">
  Loading awesome things...
</template>

<template name="MasterLayout">
    <div layout horizontal center-center fit id="content">
        {{> yield}}
    </div>
</template>
  

尝试导航到http://localhost:3000/nothinghereyet,应该返回 Ups ...没有人在这里?

答案 1 :(得分:1)

好的,请尝试一下,让我知道它是否有效:

<template name="NotFound">

  <div class="nav-container">
    {{> yield "menu"}}
  </div>

  Ups... nobody here?

  <div class="footer-container">
    {{> yield "footer"}}
  </div>

</template>

答案 2 :(得分:1)

我创建了一个小型示例项目,向您展示它的工作原理如上所述。如果有帮助,请告诉我。

https://github.com/meteorpoly/sof-iron-router-sample

相关问题