为什么 Meteor 对这个 IronRouter 代码不满意?

Why does Meteor get cranky with this IronRouter code?

为了回应 AutumnLeonard 的评论,我尝试了该想法的简约实现。我首先通过 "meteor add iron:router" 添加了 iron router 包,然后尝试了这个代码:

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
  });
}

(前两行是我添加的唯一行;其余都是多余但无害的 "boilerplate" 默认流星应用程序遗留下来的代码)

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

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行,char 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/."

那么我需要输入什么才能使 "Oops" 消失?需要什么 Route.route()?

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

只是我的 2 美分

你听起来不开心:-( Meteor 有点学习曲线,但对我来说这是非常值得的。

我看到这里有几件事可能会让您感到困惑。

Templates vs Routes: 路由和模板的区别。

模板就像是填写一些内容的食谱HTML。在模板中嵌套模板有助于将应用程序分解成更小的部分。

一条路线就像擦掉一切重新开始。您实际上放弃了所有 HTML,并从一个新模板开始。

这种差异来自于以前构建网络应用程序的方式,现在仍然非常有用。

模板包含:您不会使用这样的路由:{{> thought }}。这是包含模板的语法。
这将导入 HTML 模板(就像您定义的那样)。你不需要一条路线来完成这项工作。

Routes: 在这里,routes 定义了顶级模板。他们擦除所有内容(Session 变量除外)并重新开始。

路径很重要,因为它标识了应用程序中的位置。这让用户可以在应用程序中为位置添加书签。

两条路线具有相同的路径肯定是错误的。哪个应该用于书签?删除其中一条路线继续前进。

Body:您不能像在 HTML 顶部那样在 body 中填充东西。 (你可以,但这不是最佳实践:-) Meteor 基本上将路由模板附加到标记中。不定义 <body> 令人不快,但这就是它的工作原理。

<body>更改为<template name="main">,并修复</body>

然后将模板添加到路由中:

Router.route("/", {template: "main"});

这可能不会 100% 有效,但它应该可以帮助您克服目前遇到的一些障碍。

还有,放松心情,玩得开心!从PHP和Angular/Express过来后,Meteor很好玩!

您可以试试 Discover Meteor 这本书。这对我来说是一个很好的入门方式。刚开始用了几天。

事实证明它比我尝试的更容易;大多数花哨的裤子都是臭味。

我在 *.js 文件中需要的是:

Router.route('/');
Router.route('/thought');
Router.route('/anotherthought');

下面的 HTML 按预期工作(仅显示 "localhost:3000" 的 H1,并在附加“/thought”或“/anotherthought”时显示除 H1 之外的适当模板到那个基地 URL.

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

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

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

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

所以要创建一个我可以添加我可以共享的内容的流星应用程序,我需要做的就是:

0) 在命令提示符下输入“meteor create <projectName>”,如:“meteor create thoughts” =18=]

1) cd'ing 到目录(即项目名称)后,按照指示输入“meteor add iron:router

2) 为了让您的 "raw" URL 不会抛出异常,请将其添加到您的 .js 文件中:

Router.route('/');

3) 每次我想制作一些东西时,向 .html 文件添加一个新模板 public.

4) 然后(每次),在.js文件中添加一个路由指令,模板名称;例如,如果您添加此模板:

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

...您可以将此行添加到 .js 文件中:

Router.route('/thought');

5) 通过在命令提示符下输入“meteor deploy ”来制作你的 meteor 应用 public;例如,您可以输入“meteor deploy thoughts”或“meteor deploy rompecabeza”(部署的名称不必与项目名称匹配) .

6) 向 "point their browser at" 你的 URL 推荐任何你想看的人,例如,“嘿,帮派!去捷克 out '<your URL, with the appended template name>' !以后可以给我一分钱!"

仅此而已。

例如,我创建了一个静态站点来提供我的几张照片。可以通过以下链接查看它们:

http://dplatypus.meteor.com/pinnacles
http://dplatypus.meteor.com/garrapatabeach
http://dplatypus.meteor.com/garrapataturnout
http://dplatypus.meteor.com/mcwayfalls
http://dplatypus.meteor.com/pfeifferbeach

或者,您也可以从 dplatypus.meteor.com

点击上面的链接