【问题标题】:Router.go( ) page render in MeteorRouter.go() 页面在 Meteor 中渲染
【发布时间】:2015-07-21 07:02:09
【问题描述】:

当我点击下面触发的/login 路由时,会呈现正确的路由,但页面顶部以我的template 开头,而不是我的layoutTemplate。我必须向上滚动才能看到路由中配置的layoutTemplate

   if(! Meteor.userId()){
        Session.set('single_post_not_logged_in', Session.get('post_id'));
        Router.go('/login');
    }

这是被触发的路由:

Router.route('login', {
    path: '/login',
    layoutTemplate: 'loginLayoutTemplate',
    template: 'login_template',
    onBeforeAction: function(){
        if(Meteor.loggingIn()){
            var post_visited = Session.get('single_post_not_logged_in');
            console.log(post_visited);
            if(post_visited != undefined){
                Router.go('/post/' + post_visited);
            }

        }
        this.next();
    }
});

最后,这是路线离开的布局模板 (layoutTemplate) 和新的布局模板 (loginLayoutTemplate)

<body>
    {{renderPage}}
</body>

<template name = 'layoutTemplate'>
    {{> loginButtons}}
    <a id = 'home_button' href="{{pathFor 'home'}}"><h1 id='layout_header'>snippetExchange</h1></a>
    <div id='layout_header_row_2'>
        <h2 id='layout_by_line'>valuable answers</h2> 
        <a id = 'new_post_button' href="{{pathFor 'new_post'}}">new post</a>
    </div>
    {{> yield}}
</template>

<template name = 'loginLayoutTemplate'>
    <a id = 'home_button' href="{{pathFor 'home'}}"><h1 id='layout_header'>snippetExchange</h1></a>
    <div id=''>
        <h2 id='layout_by_line'>valuable answers</h2> 
    </div>
    {{> yield}}
</template>

<template name = 'login_template'>
    <h3>please login or create an account</h3>
    {{> loginButtons}}
</template>

【问题讨论】:

  • template: login_template 但您的 HTML 中没有 login_template?检查检查器是否有错误。 template 将在layoutTemplate 定义的模板中重装{{&gt; yield}}
  • 我有一个login_template,我把它放在我的帖子底部
  • 很奇怪,今天出现但昨天不可见。现在我重新阅读了您的问题,实际上似乎路线正在正确呈现,但垂直滚动位置设置为 template 的顶部,对吗?使用模板助手和一些 javascript 很容易解决这个问题,但为什么我无法解决更深层次的问题。
  • 我在您的评论之后添加了 login_template 代码。我很困惑如何需要模板助手来修复滚动?

标签: javascript meteor iron-router spacebars


【解决方案1】:

试试:

Template.login_template.onRendered(function(){
  window.scrollTo(0, 0);
});

【讨论】:

  • 嗯。我被警告过好几次,期望模板的渲染次数比预期的要多。这会导致一些意外的页面滚动吗?
  • 助手会被多次调用,但给定的模板应该只渲染一次。 除非它嵌套在{{#each}}!
猜你喜欢
  • 1970-01-01
  • 2014-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-06
  • 1970-01-01
  • 2012-10-10
相关资源
最近更新 更多