nt.*_*bas 1 meteor iron-router
TL,DR:给定路由器仅在客户端运行的情况下,如何使未找到的模板在全局布局模板中工作(这是要求:客户端和服务器路由是分开的。)
我需要iron:router在无法与任何路由器匹配时显示默认模板。
在我的应用程序中,所有路由器都是客户端(router.js文件位于客户端文件夹中。)
到目前为止,这是我的代码:
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'});
}
});
});Run Code Online (Sandbox Code Playgroud)
<template name="ApplicationLayout">
<div class="nav-container">
{{> yield "menu"}}
</div>
<div class="main-container">
{{> yield}}
</div>
<div class="footer-container">
{{> yield "footer"}}
</div>
</template>Run Code Online (Sandbox Code Playgroud)
在http://eventedmind.github.io/iron-router/#404s-and-client-vs-server-routes上阅读文档,服务器似乎发送了,404但不是。因此,我在服务器端制作了另一个路由器,该路由器发送,404但没有任何问题。
Router.map(function() {
this.route('notFound', {
path: '*',
action: function() {
this.response.statusCode = 404;
this.response.end();
}
});
});Run Code Online (Sandbox Code Playgroud)
到目前为止的结果:
Oops, looks like there's no route on the client or the server for url: "http://localhost:3000/logi/."
其他尝试:
我试过,包括notFoundTemplate在全球路由器配置和它没有显示的NotFound模板,但没有在该中layoutTemplateMenu和Footer模板不加载。但是如果我理解正确,则data函数返回时会调用此类模板-httpsnull : //github.com/EventedMind/iron-router/issues/116,请参阅cmather的答案。
相关细节:
使用notFoundTemplate在配置属性确实显示的 NotFound模板,但省略了Menu和Footer模板。
小智 5
您需要的是:
在路由器配置中定义常规模板(服务器和客户端位置)
Router.configure({
layoutTemplate: "MasterLayout",
loadingTemplate: "Loading",
notFoundTemplate: "NotFound"
});
Run Code Online (Sandbox Code Playgroud)
在客户端文件夹下定义常规模板
<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>
Run Code Online (Sandbox Code Playgroud)
尝试导航到例如http:// localhost:3000 / nothinghereyet,它应该返回Ups ...这里没有人吗?
| 归档时间: |
|
| 查看次数: |
2328 次 |
| 最近记录: |