【问题标题】:Iron Router - Routers no longer working (v1.3)Iron Router - 路由器不再工作 (v1.3)
【发布时间】:2016-07-12 22:00:40
【问题描述】:

我以为我把铁路由器钉牢了,但由于某种原因,我遇到了问题,它没有显示根默认模板之外的其他视图 - 而且它过去工作正常。

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

你可以在这里看到:

placelist.meteorapp.com - 主要

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);
              }

});

【问题讨论】:

    标签: meteor meteor-blaze


    【解决方案1】:

    Router.map 在 Iron Router 中已弃用。您现在应该使用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/

    【讨论】:

    • 嗨,好的,下面似乎是最终修复。使用 except 似乎可以完成“在这种情况下使用此默认布局”之类的工作。无论如何,感谢您为我指明正确的方向...
    【解决方案2】:

    这是最终代码:

    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);
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-09
      • 2016-02-26
      • 2018-11-21
      • 1970-01-01
      • 2014-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多