node.js Express - 如何异步获取部分视图 [英] node.js Express - How to get partial views asynchronously
问题描述
如果我可能弄错这个网页架构,请指导我。
@drachenstern表示,您只想渲染部分HTML片段,而不是整个文档,包括布局。您可以通过以下方式告诉express:跳过布局:
res.render('sometemplate',{layout:false});
如果要查找与全页浏览器加载不同的Ajax请求,请使用 req.xhr
标志如下所示
因此,您甚至可以执行
res.render ('sometemplate',{layout:!req.xhr});
I've got a layout - navigation menu. In express tutorials theres only old-school pages loading. whole old page is thrown away and a new one is downloaded with all layouts,views and partial views. And i want navigation menu to stay. So how can i do that?
If i'm maybe getting smth wrong with this web pages architecture please guide me.
As @drachenstern said, you want to render only partial HTML fragments, not whole documents including the layout. You can tell express to skip the layout using:
res.render('sometemplate', {layout: false});
If you want to look for Ajax requests as distinct from full-page browser loads, use the req.xhr
flag as documented here
Thus you might even be able to do
res.render('sometemplate', {layout: !req.xhr});
这篇关于node.js Express - 如何异步获取部分视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!