node.js Express - 如何异步获取部分视图 [英] node.js Express - How to get partial views asynchronously

查看:140
本文介绍了node.js Express - 如何异步获取部分视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个布局 - 导航菜单。在快速教程中,只有旧版页面加载。整个旧页面被丢弃,并且下载了一个新的页面,其中包含所有布局,视图和部分视图。我想要导航菜单留下来。那我该怎么办呢?



如果我可能弄错这个网页架构,请指导我。

解决方案

@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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆