如何在 Hapi JS 中设置 Flash 消息?
How to set Flash Messages in Hapi JS?
好吧,我尝试使用 hapi-flash,但它对我不起作用。所以这里有人在 Hapi JS 中使用 flash 消息
是的,您需要使用 Yar。将其注册为插件后,您可以在每个处理程序中使用:
request.session.flash('error', 'There was an error.');
要获取即显消息,请使用 request.session.flash('error')。这将 return 当前在闪存中的所有 'error' 消息。它还将清除闪存 - 细节可以在 repo 上找到。
我发现它对我们 onPreResponse 扩展很有帮助,可以获取所有的即时消息并将它们默认添加到上下文中。如果您结束此操作,请确保在注册 yar 之前注册扩展点。
假设您 api/website 正在服务器上注册为插件:
exports.register = function (server, options, next) {
server.ext('onPreResponse', internals.onPreResponse);
server.register([
{
register: require('yar'),
options: {
cookieOptions: {
password: process.env.SECRET_KEY
}
}
}
], function (err) {
Hoek.assert(!err, 'Failed loading plugin: ' + err);
next()
};
internals.onPreResponse = function (request, reply) {
var response = request.response;
if (response.variety === 'view') {
if (!response.source.context) {
response.source.context = {};
}
// This can be slimmed down, but showing it to be explicit
var context = response.source.context;
var info = request.session.flash('alert');
var error = request.session.flash('error');
var notice = request.session.flash('notice');
var success = request.session.flash('success');
context.flash = {};
if (info.length) {
context.flash.info = info;
}
if (error.length) {
context.flash.error = error;
}
if (notice.length) {
context.flash.notice = notice;
}
if (success.length) {
context.flash.success = success;
}
return reply.continue();
}
return reply.continue();
};
处理程序看起来像这样:
exports.login = {
handler: function (request, reply) {
// Do login stuff here
request.log(['error', 'login'], err);
request.session.flash('error', 'There was an error logging in. Try again.');
return reply.redirect('/');
},
auth: false
};
好吧,我尝试使用 hapi-flash,但它对我不起作用。所以这里有人在 Hapi JS 中使用 flash 消息
是的,您需要使用 Yar。将其注册为插件后,您可以在每个处理程序中使用:
request.session.flash('error', 'There was an error.');
要获取即显消息,请使用 request.session.flash('error')。这将 return 当前在闪存中的所有 'error' 消息。它还将清除闪存 - 细节可以在 repo 上找到。
我发现它对我们 onPreResponse 扩展很有帮助,可以获取所有的即时消息并将它们默认添加到上下文中。如果您结束此操作,请确保在注册 yar 之前注册扩展点。
假设您 api/website 正在服务器上注册为插件:
exports.register = function (server, options, next) {
server.ext('onPreResponse', internals.onPreResponse);
server.register([
{
register: require('yar'),
options: {
cookieOptions: {
password: process.env.SECRET_KEY
}
}
}
], function (err) {
Hoek.assert(!err, 'Failed loading plugin: ' + err);
next()
};
internals.onPreResponse = function (request, reply) {
var response = request.response;
if (response.variety === 'view') {
if (!response.source.context) {
response.source.context = {};
}
// This can be slimmed down, but showing it to be explicit
var context = response.source.context;
var info = request.session.flash('alert');
var error = request.session.flash('error');
var notice = request.session.flash('notice');
var success = request.session.flash('success');
context.flash = {};
if (info.length) {
context.flash.info = info;
}
if (error.length) {
context.flash.error = error;
}
if (notice.length) {
context.flash.notice = notice;
}
if (success.length) {
context.flash.success = success;
}
return reply.continue();
}
return reply.continue();
};
处理程序看起来像这样:
exports.login = {
handler: function (request, reply) {
// Do login stuff here
request.log(['error', 'login'], err);
request.session.flash('error', 'There was an error logging in. Try again.');
return reply.redirect('/');
},
auth: false
};