繁体   English   中英

如何将路由异步添加到铁路由器?

[英]how can i add routes asynchronously to iron router?

我在lib/server/plugins.js有一个流星方法

Meteor.methods({
  getPlugins: function() {
    return [
      { path: 'test' },
      { path: 'test2' }
    ]
  }
});

和我的路由器配置文件在lib/routes.js

Router.route('/', function() {
    this.render('home');
}); 

Meteor.call('getPlugins', function(e,r) {
    if(!e) {
        for(var plugin in r) {
            function() {
                this.route(r[plugin].name);
            })
        }
    } else {
        console.log(e);
    }
})

var routes  = [
    { path: '/test3' },
    { path: '/test4' }
]

for(var i in routes) {
    Router.map(function() {
        this.route(routes[i].path);
    });
}

本地变量路由中的所有路由都可以正常工作,但是来自getPlugins铁路由器的路由说“ Oops, looks like there's no route on the client or the server for url: "http://localhost:3000/test2."

尝试此操作,首先在路由之前添加/:

Meteor.methods({
  getPlugins: function() {
    return [
      { path: '/test' },
      { path: '/test2' }
    ]
  }
});

然后修改此this.route(r[plugin].name); Router.route(r[plugin].path)

感谢Kyll,我想出了这个解决方案,它可以完成工作,但是我仍然对子路径有疑问

Router.route('/:plugin', function() {
    var that = this;
    Meteor.call('getPlugins', function(e,r) {
        if(!e) {
            for(var plugin in r) {       
                if(r[plugin].path == that.params.plugin) {
                    that.render(that.params.plugin);
                }
            }
        } else {
            console.log(e);
        }
    })
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM