您可以使用以下方法向 View 公开数据:app.local 和 res.locals。有人确切知道两者之间的区别是什么吗?谢谢
最佳答案
来自 express.js API 文档:
应用本地化
Application local variables are provided to all templates rendered within the application. This is useful for providing helper functions to templates, as well as app-level data.
资源本地化
Response local variables are scoped to the request, thus only available to the view(s) rendered during that request / response cycle, if any. Otherwise this API is identical to app.locals.
这意味着唯一的区别是 res.locals 仅在请求的生命周期内有效。
关于Node.js Express 3.x 将变量传递给 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11770425/