我正在尝试 Sails.js beta 版本(1.0.0-46),并注意到 Flash 消息无法开箱即用:
req.flash(type, message)
我在尝试使用它时收到了 TypeError: req.flash is not a function 消息。
哎呀...刚刚在升级文档中找到了答案(升级到 v1.0)。
connect-flash 中间件已被删除(因此默认情况下 req.flash() 将不再可用)。如果您希望继续使用 req.flash(),请在应用程序文件夹中运行 npm install --save connect-flash 并手动添加中间件。
运行以下命令:
npm install --save connect-flash
并修改config/http.js文件:
middleware: {
flash : require('connect-flash')(),
order: [
'cookieParser',
'session',
'flash', // <-- add this
// 'bodyParser',
'compress',
'poweredBy',
'router',
'www',
'favicon',
],
它在它的会话对象中
req.session.flash
,你可以将它用作
req.session.flash{type:'success', message:'your message'}