【发布时间】:2016-06-05 20:25:32
【问题描述】:
我正在尝试根据 MeteorJS 中路由上的 URL 参数返回不同的数据。
在 nodejs 的背景下,我会这样做:
testPage = function (req, res, next) {
//Got parameter query from url
console.log('Getting at /testPage/:query '+req.params.query);
//do something with that parameter, say make an HTTP call or query database
var newThing = req.params.query+"hi";
// render the page
var data = {
foo: newThing
};
res.render('testPage', data);
};
Meteor 不支持服务器端渲染,所以不支持。我还在纠结 Meteor 的单页客户端渲染;我应该如何在 Meteor 中解决这个问题?
我的第一次尝试(使用 IronRouter):
Router.route('/testPage/:query', function () {
console.log("Got param at /testPage/:query "+this.params.query);
if(Meteor.isServer){
console.log("This code may never be called?");
//Otherwise, make an HTTP call
// and then somehow pass the data back to the client...
}
this.render("query");
});
这是我可以用反应变量做的事情吗?或者也许从客户端向单独的服务器端点进行 AJAX 调用?
【问题讨论】:
-
我建议当路由改变时在客户端获取查询参数。然后使用新参数调用服务器方法。 Router.current() 是响应式的,因此您可以在自动运行中检查其参数,并将您的方法调用也放入其中。如果我有时间,我会尝试制作一个 Meteorpad 来演示。
标签: meteor client-server single-page-application iron-router