javascript - 为什么 Meteor 对这个 IronRouter 代码感到暴躁?

标签 javascript meteor iron-router stderr

回应 AutumnLeonard 的评论 here ,我尝试了这个想法的简约实现。我首先通过“meteor add iron:router”添加了 Iron 路由器包,然后尝试了以下代码:

blogtest.html:

<head>
  <title>blogtest</title>
</head>

<body>
  <h1>This is really something, isn't it!?!</h1>

  {{> thought}}
  {{> anotherthought}}
</body>

<template name="thought">
  <p>THis is a random thought.</p>
</template>

<template name="anotherthought">
  <p>THis is another random thought.</p>
</template>

blogtest.js:

Router.route("/:blog_post_title", {template: "thought", name: "thought"});
Router.route("/:blog_post_title", {template: "anotherthought", name: "anotherthought"});

if (Meteor.isClient) {
  // counter starts at 0
  Session.setDefault('counter', 0);

  Template.hello.helpers({
    counter: function () {
      return Session.get('counter');
    }
  });

  Template.hello.events({
    'click button': function () {
      // increment the counter when button is clicked
      Session.set('counter', Session.get('counter') + 1);
    }
  });
}

if (Meteor.isServer) {
  Meteor.startup(function () {
    // code to run on server at startup
  });
}

(最上面的两行是我添加的唯一一行;其余的都是默认 meteor 应用程序留下的多余但无害的“样板”代码)

...但是在尝试运行它时,它失败了,命令提示符处发出蓝色和紫色的咆哮声,即:

W20151007-09:25:00.634(-7)? (STDERR) Error: A route for the path "/:blog_post_ti
tle" already exists by the name of "anotherthought".

(如果我的 IronRouter 语法在这里错误,为什么它会提示“anotherthought”而不是“another”?)

W20151007-09:25:00.635(-7)? (STDERR)     at blogtest.js:2:8

(第2行,第8个字符是第二行“Router.route”中的“r”......???)

W20151007-09:25:00.635(-7)? (STDERR)     at C:\Misc\blogtest\.meteor\local\build
\programs\server\app\blogtest.js:37:4

(blogtest.js 中没有第 37 行……???)

更新

好的,所以我将 HTML 更改为:

<head>
  <title>blogtest</title>
</head>

<body>
  <h1>Here's a thought:</h1>

  <!-- {{> thought}}
  {{> anotherthought}} -->
</body>

<template name="thought">
  <p>This is a random thought.</p>
</template>

<template name="anotherthought">
  <p>This is another random thought.</p>
</template>

...以及路由代码:

Router.route("/thought", {template: "thought", name: "thought"});
Router.route("/anotherthought", {template: "anotherthought", name: "anotherthought"});

...并且它不再运行失败;事实上,当我输入“http://localhost:3000/thought”时,我确实看到了我所期望的,即:

Here's a thought:

This is a random thought.

...以及我对“http://localhost:3000/anotherthought ”的期望,即:

Here's a thought:

This is another random thought.

但是,我在 localhost:3000(默认 URL)处看到了这个:

Here's a thought:

Oops, looks like there's no route on the client or the server for url: "http://localhost:3000/."

那么我需要输入什么才能让“哎呀”消失?需要什么 Route.route() ?

最佳答案

您定义了两个相同的路线(“/:blog_post_title”),我猜不能相同。也许你可以尝试改变其中之一。也许您可以将另一个定义为“/:blog_post_title2”。

只是我的2美分

关于javascript - 为什么 Meteor 对这个 IronRouter 代码感到暴躁?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32997950/

相关文章:

javascript - c3js 的缩放功能是否也可以放大 y 值?

javascript - CSS - 悬停时向左移动图像一点点,如果没有悬停则返回原始位置

javascript - Meteor 在带有 setTimeout 的 Fiber 中运行

meteor + react : Server side routes?

javascript - Meteor Router中的异步调用闪烁其他模板

meteor iron-router 并获取 accounts-entry 包的所有路由的名称

javascript - 字符串包含价格吗?

javascript - 如何使用 jQuery 分离和重新附加不同列表中不同类的项目?

javascript - 将值从 html 传递到 AngularJS Controller

meteor - 如何在 meteor js中捕获引导模式打开/关闭事件