TIM*_*MEX 4 javascript node.js express
app.get('/',function(req,res){
res.render('home'); // I want the template to be able to access the flash message..
});
app.get('/go',function(req,res){
req.flash("info", "You went GO, and got redirected to home!");
res.redirect('/');
});
Run Code Online (Sandbox Code Playgroud)
用户首先进入"/ go".之后,他将被重定向到"/",我想要一条flash消息显示为javascript警报.
我怎样才能做到这一点?
Tod*_*ell 10
将其作为本地添加到您的渲染调用中:
res.render("home", {info: req.flash("info")});
Run Code Online (Sandbox Code Playgroud)
并在您的模板中使用它:
#flash
p= info
Run Code Online (Sandbox Code Playgroud)
您可以使用快速动态助手让您的生活更轻松.
app.dynamicHelpers({flashMessages: function(req, res) {
var html = ""
, flash = req.flash();
['error', 'info'].forEach(function(type) {
if(flash[type]) {
flash[type].forEach(function(message) {
html += "<div class='alert " + type + "'>" + message + "</div>";
});
}
});
return html; }});
Run Code Online (Sandbox Code Playgroud)
并在你的布局(ejs例子)
<body><%- flashMessages %><%- body %></body>
Run Code Online (Sandbox Code Playgroud)
app.dynamicHelpers({flash: function(req, res){return req.flash();}});
Run Code Online (Sandbox Code Playgroud)
现在您可以访问视图中的 Flash 对象。您的逻辑中没有 HTML,也无需在每个路由中添加所有参数。
| 归档时间: |
|
| 查看次数: |
9207 次 |
| 最近记录: |