在Meteor.js中通过同一路线内的锚点标签更新视图

Update view via anchor tag within the same route in Meteor.js

本文关键字:一路 新视图 更新 标签 js Meteor      更新时间:2023-10-05

我一直在Meteor.js中使用URL参数实现分页。但当我单击锚定标签移动到新页面时,应用程序不会更新其视图。单击其他标记时,视图将正确更新。

这种行为真的很奇怪,因为(1)至少它能识别onClick(我通过附加事件处理程序检查了这一点)(2)尽管视图在第二次尝试时更新了,但它必须是第三个元素。(假设我在第1页。如果我点击第2页,它不会改变。如果我一次又一次地点击第2页,它不会改变。但当我点击第3页时,它会改变。)

我认为问题出在Router.route附近,因为除了同一条路线之外,它运行得很好。

client.js

Router.route('/', {
  name: 'index',
  template: 'index',
  layoutTemplate: 'ApplicationLayout',
  waitOn: function() {
    var page = getQueryParams(window.location.search).page;
    page = page === undefined? 1: page;
    return Meteor.subscribe('recentPosts', page);
  },
  data: function() {
    return {
      posts: Posts.find({}, {
        sort: {publishedAt: -1}
      })
    };
  },
  onBeforeAction: function() {
    Meteor.call('postsCount', function(e, r) {
      Session.set('postsCount', r);
    });
    this.next();
  }
});

分页.html

<template name="pagination">
  <li class="page {{#if isCurrent page}}active{{/if}}">
    <a href="/?page={{page}}">
      {{page}}
    </a>
  </li>
</template>

我的URL方案是domain/?page=<page_id>。此外,我的路线在刷新时运行良好。

为了解决您的问题,我怀疑您需要更改在waitOn函数中获取当前页面的方式:

var page = this.getParams().query.page;

getParams()是被动的,而this.params不是(你也没有使用——你应该经过路由器),所以地址栏中的路由正在更改,但订阅没有更新。