流星路由器布局渲染两次
问题描述:
我不知道为什么,但我的布局渲染了两次。流星路由器布局渲染两次
这里是我的index.html:
<head>
<title>title</title>
</head>
<body>
{{>layout}}
</body>
这里是我的布局:
<template name="layout">
{{#if canShow}}
{{>Template.dynamic template=content}}
{{else}}
{{> loginButtons}}
{{/if}}
</template>
所以在这里没有我的路由模板显示只是一个时间。
这里是我的路线:
FlowRouter.route('/', {
action() {
BlazeLayout.render("layout", {
content: "home"
});
}
});
但随着这条路线我的模板是显示第二次。
这是我的佣工,我认为这与这个问题无关,但我们永远不知道。
Template.home.onCreated(function() {
this.autorun(() => {
this.subscribe('post');
});
});
Template.layout.helpers({
canShow() {
return !!Meteor.user();
}
});
Template.home.helpers({
cats() {
return Posts.find({});
}
});
答
您不需要在正文中渲染布局。
路由器将负责渲染。
如此,只是有
<body>
</body>
或者甚至不用它。
编辑:感谢基思,我对自己的问题有了更好的理解。这里是他的评论:
有一点要记住,你在流星中写的所有html都不保存为html。它都被转换为javascript。像index.html这样的东西不会被推送到浏览器。流星只是把你写的所有html转换成javascript,并根据你的代码所描述的呈现它需要的东西。这是知道如何动态地改变和重新生成html
对于像更改标题头或添加元等等,我们可以直接在javascript中做。