为什么渲染模板后回家的路线会发生变化?

Why does route to home changes after rendering a template?

我刚刚开始使用 iron:router 包。这些是我的项目文件:

路由器-example.js

    if (Meteor.isClient) {
    //some code
    }
    if (Meteor.isServer) {
   //some code
    }
    Router.route('/', function(){
        this.render('Home');
    });
    Router.route('/hello', function(){
        this.render('hello');
    });
    Router.route('/items', function(){
        this.render('Items');
    });
    Router.route('/serverItem', function(){
        var req = this.request;
        var res = this.response;
        res.end('Hello from the server\n');
    }, {where: 'server'});

路由器-example.html

<body>
<h1>Welcome to Meteor!</h1>
<ol>
    <li><a href="{{pathFor '/'}}">This routing doesn't work</a></li>
    <li><a href="{{pathFor 'hello'}}">Hello Template</a></li>
    <li><a href="{{pathFor 'items'}}">Items Template</a></li>
    <li><a href="{{pathFor 'serverItem'}}">Server Item</a></li>
    <li><a href="http://localhost:3000/">Hard link works</a></li>
</ol>
</body>

templates.html

<template name = "Home">
<h2>Default: '/' brings you here</h2>
<p>This is the home template</p>
</template>

<template name = "Items">
<h2>This is the items template. Items Page linked using pathFor helper</h2>
</template>

<template name="hello">
<button>Click Me</button>
<p>You've pressed the button {{counter}} times.</p>
</template>

因此在主页 "localhost:3000" 中,"Home" 模板默认呈现,正如预期的那样。单击其他 link 后: 你好模板, 项目模板等 这些已呈现,但使用 {{pathFor '/'}} 助手指定的主页 link 停止工作,我必须使用硬 link (localhost:3000) 才能返回主页。将鼠标悬停在 link 上表明它指向不同的路线。

那么我做错了什么?

您可以指定路由名称以便使用{{pathFor 'routeName'}}:

Router.route('/', { name: 'home', template: 'Home' })

在此处查看完整示例https://github.com/iron-meteor/iron-router/blob/devel/Guide.md#route-specific-options

If no name is provided, the router guesses a name based on the path