首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何向iron路由器异步添加路由?

如何向iron路由器异步添加路由?
EN

Stack Overflow用户
提问于 2015-03-10 23:12:13
回答 2查看 93关注 0票数 0

我在lib/server/plugins.js中有一个meteor方法

代码语言:javascript
复制
Meteor.methods({
  getPlugins: function() {
    return [
      { path: 'test' },
      { path: 'test2' }
    ]
  }
});

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

代码语言:javascript
复制
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 iron路由器的路由显示为Oops, looks like there's no route on the client or the server for url: "http://localhost:3000/test2."

EN

回答 2

Stack Overflow用户

发布于 2015-03-10 23:48:26

尝试一下,首先在路由之前添加一个/:

代码语言:javascript
复制
Meteor.methods({
  getPlugins: function() {
    return [
      { path: '/test' },
      { path: '/test2' }
    ]
  }
});

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

票数 0
EN

Stack Overflow用户

发布于 2015-03-11 01:22:11

多亏了Kyll,我想出了这个解决方案,它确实起到了作用,但我仍然对子路径有疑问

代码语言:javascript
复制
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);
        }
    })
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28967351

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档