铁路由器 - 路由器不再工作(v1.3)

时间:2016-07-11 23:42:49

标签: meteor meteor-blaze

我以为我已经固定了铁路由器,但出于某种原因,我遇到的问题是它没有显示超出根默认模板的其他视图 - 而且它曾经工作正常。

基本上我有一个主视图的默认布局模板。然后我有一个基本上显示不同用户列表的动态路由。

你可以在这里看到:

placelist.meteorapp.com - main

placelist.meteorapp.com/cmeelater - 动态用户列表

主视图完全正常(具有登录和注销状态),但动态子路径(/ name-of-list)的模板未显示 - 而是显示默认模板。

之前有效,但现在我有点迷失了。任何帮助将非常感激。 (我一直在寻找信息)。

添加个人信息:

我的 route.js 中发生的事情是会话变量从URL参数传递给查询。然后根据URL参数拉出该用户的列表,并使用“userListsShared”模板显示此信息(如果我只是盲目地将{{> userListsShared}}插入主模板,这是有效的,但我不希望这样)。

我想如果你宣布模板在路由中使用它会使用该模板 - 是否有一些继承的事情发生?

Router.configure({
    layoutTemplate: 'userListsShared',
    loadingTemplate: 'loading',
    waitOn: function() { 
      return Meteor.subscribe('posts'); 
      return Meteor.subscribe('userposts');
    }
});

Router.map(function() {
    this.route('main', {
              path: '/',
              template: 'main'
            }
      );
    this.route('shared', {
      path: '/:sharelink',
      template:'userListsShared',
      data: function () 
              {
                   Session.set('curShareList', this.params.sharelink);
              }

});

2 个答案:

答案 0 :(得分:0)

Iron Router中不推荐使用

Router.map。您现在应该使用Router.route来定义每条路线。另外,不要使用data方法来设置会话变量。该方法用于定义页面上下文,并期望返回一个对象。使用onBeforeAction方法,如下所示:

// If the route name and template are the same, you can just specify the name.
Router.route('/', {
  name: 'main'
})

Router.route('/:sharelink', function () {
  name: 'shared',
  template: 'userListsShared'
})

// The 'only' property is a list of what routes the function will be called before.
Router.onBeforeAction(
  function () {
    Session.set('curShareList', this.params.sharelink);
  },
  only: ['shared'] // You could also use "except: ['main']" instead
});

查看官方指南;它引导您完成整个过程:http://iron-meteor.github.io/iron-router/

答案 1 :(得分:0)

这是最终的代码:

Router.configure({
    layoutTemplate: 'main',
    loadingTemplate: 'loading',
    waitOn: function() { 
      return Meteor.subscribe('posts'); 
      return Meteor.subscribe('userposts');
    }
});


Router.route('/', function () {
    this.layout('main');
    this.render('main');
    except:['userListsShared']
});

Router.route('/:sharelink', function () {
    this.layout('userListsShared');
    this.render('userListsShared');
    var thisuserlist = this.params.sharelink;
   //var routeName = Router.current().route.getName();
   //console.log("routeName = " + routeName);
  Session.set('curShareList', this.params.sharelink);
});