代码:
登录.ejs
<script>
req.flash('success_msg', 'You have logged in');
</script>
标题.ejs
<div class = "alertMessage">
<% if (success_msg != false){ %>
<span class="alert alert-success containerMargins">
<%= success_msg %>
</span>
<% } %>
<% if (error_msg != false){ %>
<span class="alert alert-danger containerMargins">
<%= error_msg %>
</span>
<% } %>
</div>
情况:
这与在服务器端使用 Flash 并在客户端显示消息无关:它已经非常适合我了。
这与从客户端调用 flash 或使用其他库从客户端复制相同的行为有关。
问题:
我展示的代码当然不适用于客户端,我可以做什么来在客户端复制该行为?
最佳答案
The flash is a special area of the session used for storing messages. Messages are written to the flash and cleared after being displayed to the user. The flash is typically used in combination with redirects, ensuring that the message is available to the next page that is to be rendered.
所以你需要的代码是:
- 将一些数据存储在客户端可以在页面之间访问的位置
- 读取该数据
- 阅读后删除
从选择somewhere开始对于选项 1(例如 localStorage 或 cookie)。其余的应该是微不足道的 - 原始模块是关于 80 lines of code, including about 50% comments — 实现(但具体取决于您做出的选择)。
关于javascript - 客户端如何调用flash?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41696629/