为什么流星会用这个铁路由器代码变得暴躁
Why does Meteor get cranky with this IronRouter code?
为了回应AutumnLeonard在这里的评论,我尝试了这个想法的简约实现。我首先通过"meteor add iron:router"添加了铁路由器包,然后尝试了以下代码:
博客测试.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>
博客测试.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 语法在这里是错误的,为什么它会抱怨"另一个想法"而不是"另一个"?
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中没有第37行.js...???(
更新
好的,所以我将 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美分
你听起来不开心:-( 流星是一个学习曲线,但对我来说这是非常值得的。
我在这里看到一些可能会让你绊倒的事情。
模板与路由:路由和模板之间的区别。
模板就像填写一些 HTML 的配方。 在模板中嵌套模板有助于将应用分解为更小的部分。
一条路线就像抹去一切,然后重新开始。 您基本上删除了所有 HTML,并从新模板开始。
这种差异来自以前的Web应用程序的构建方式,现在仍然非常有用。
模板包括:您不会使用这样的路由:{{> thought }}
. 这是包含模板的语法。
这将导入 HTML 模板(就像您定义的那样(。 您不需要路由来完成此操作。
:在这里,路由定义了顶部模板。 他们擦除所有内容(会话变量等例外(并重新开始。
路径很重要,因为它标识应用中的位置。 这允许用户为应用中的地点添加书签。
拥有两条具有相同路径的路由肯定是一个错误。 哪个应该用于书签? 删除其中一条路线以继续前进。
正文:你不能像在HTML顶部那样在正文中填充东西。 (好吧,你可以,但这不是最佳实践:-( 流星基本上将路由模板附加到标签中。 不定义<body>
是很不和谐的,但这就是它的工作原理。
将<body>
更改为 <template name="main">
,并修复</body>
然后将模板添加到路由:
Router.route("/", {template: "main"});
这可能不是 100%,但它应该让你通过你遇到的一些障碍。
另外,放松一下,玩得开心! 从PHP和Angular/Express出来之后,Meteor很有趣!
你可以试试《发现流星》一书。 这对我来说是一个很好的入门方式。 只是花了几天时间就开始了。
事实证明,这比我尝试的要容易; 大多数花哨的裤子都是污水。
我在 *.js 文件中需要的只是:
Router.route('/');
Router.route('/thought');
Router.route('/anotherthought');
以下 HTML 按预期工作(仅显示"localhost:3000"的 H1,并在我将"/thought"或"/anotherthought"附加到该基本 URL 时,除了 H1 之外还显示相应的模板。
<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>
",如:">流星创造思想">
1(cd到目录(即项目名称(后,按照指示输入">流星加铁:路由器">
2(为了使您的"原始"URL不会引发铁异常,请将其添加到.js文件中:
Router.route('/');
3(每次我想公开某些内容时,都会向.html文件添加新模板。
4(然后(每次(,在.js文件中添加一个带有模板名称的路由指令;例如,如果您添加此模板:
<template name="thought">
<p>This is a random thought.</p>
</template>
。您可以将此行添加到.js文件中:
Router.route('/thought');
5(通过在命令提示符下输入"流星部署"来公开您的流星应用程序;例如,您可以输入">Meteor Deployment Thoughts"或">Meteor deploy rompecabeza"(部署的名称不必与项目名称匹配(。
6(建议你想看它的人"将他们的浏览器指向"你的URL,例如,">嘿,帮派!去捷克'<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
- 从桌面读取python文件时高亮显示代码
- 有没有任何方法可以将控制器从文件加载到ui路由器$stateProvider中
- AngularJS UI路由器不能像ng路由器那样工作
- 如何将函数包装在函数中以避免代码重复
- 通过单击表单中的按钮,在代码生成中使用javascript生成字母数字代码
- 货币代码为欧元-金额的格式不应包含小数
- Regex代码只允许一个空格
- 如何将屏幕分辨率乘以 80%,然后在代码中使用
- 从var向代码隐藏函数传递值
- 如何使用js代码转发reactjs路由器
- 如何减少骨干路由器中的重复代码
- Javascript代码中的访问路由器
- 如何让反应路由器响应 404 状态代码
- AngularJS路由器代码不起作用
- 更改角度 UI 路由器 2.0.13 的代码
- 将路由器代码添加到组件时分析错误?(v2.0.0-beta.0)
- 为什么流星会用这个铁路由器代码变得暴躁
- Express.js 4-路由器分区示例代码失败
- 使用Backbone.js路由器进行路由而不是使用服务器端代码的原因
- Vue.js 路由器:在组件准备就绪时运行代码