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路由器的设计更多的是一次性配置……每个路径都有自己的模板。您仍然可以使其正常工作,而无需添加更多路由。这将是另一个问题的主题。我觉得有点启发,让我搜索我能找到的,谢谢