Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将路由异步添加到iron路由器?_Javascript_Asynchronous_Meteor_Iron Router - Fatal编程技术网

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

Javascript 如何将路由异步添加到iron路由器?,javascript,asynchronous,meteor,iron-router,Javascript,Asynchronous,Meteor,Iron Router,我在lib/server/plugins.js Meteor.methods({ getPlugins: function() { return [ { path: 'test' }, { path: 'test2' } ] } }); Router.route('/', function() { this.render('home'); }); Meteor.call('getPlugins', function(e,r) {

我在
lib/server/plugins.js

Meteor.methods({
  getPlugins: function() {
    return [
      { path: 'test' },
      { path: 'test2' }
    ]
  }
});
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);
    });
}
以及
lib/routes.js中的我的路由器配置文件

Meteor.methods({
  getPlugins: function() {
    return [
      { path: 'test' },
      { path: 'test2' }
    ]
  }
});
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);
    });
}

本地变量routes中的所有路由都可以正常工作,但是来自
getPlugins
iron router的路由表示
Oops,看起来在客户端或服务器上没有url的路由:http://localhost:3000/test2.“

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

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

然后修改这个
this.route(r[plugin].name)
路由器.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);
        }
    })
});

您决定不使用
/plugins/:pluginId
常规路由而不是多个小路由,有什么具体原因吗?据我所知,Iron路由器的设计更多的是一次性配置……每个路径都有自己的模板。您仍然可以使其正常工作,而无需添加更多路由。这将是另一个问题的主题。我觉得有点启发,让我搜索我能找到的,谢谢