node.js Express - 如何异步获取部分视图
我有一个布局 - 导航菜单。在快速教程中,仅加载老式页面。整个旧页面被丢弃,并下载一个包含所有布局、视图和部分视图的新页面。我希望保留导航菜单。那么我该怎么做呢?
如果我对这个网页架构可能有误解,请指导我。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如 @drachenstern 所说,您只想渲染部分 HTML 片段,而不是包括布局在内的整个文档。您可以使用以下方法告诉express跳过布局:
如果您想查找与全页浏览器加载不同的Ajax请求,请使用
req.xhr
标志如此处记录因此您甚至可以这样做
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:
If you want to look for Ajax requests as distinct from full-page browser loads, use the
req.xhr
flag as documented hereThus you might even be able to do
您还可以使用 res.partial() ,它专门用于渲染部分。
以下是其用法示例,其中“browse.jade”是模板的名称:
You can also use res.partial() which is specifically for rendering partials.
Here is a sample of its usage, where 'browse.jade' is name of the template: