javascript - 客户端如何调用flash?

标签 javascript node.js connect-flash

代码:

登录.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.

所以你需要的代码是:

  1. 将一些数据存储在客户端可以在页面之间访问的位置
  2. 读取该数据
  3. 阅读后删除

从选择somewhere开始对于选项 1(例如 localStorage 或 cookie)。其余的应该是微不足道的 - 原始模块是关于 80 lines of code, including about 50% comments — 实现(但具体取决于您做出的选择)。

关于javascript - 客户端如何调用flash?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41696629/

相关文章:

javascript - 如果最近的元素包含字符串,则将类添加到元素

javascript - 将新代码注入(inject) DOM 的最佳方式是什么?

javascript - 如何使用异步 I/O 将实时数据集写入磁盘?

node.js - Express 4 中来自 ejs 的 Flash 消息

javascript - 转换为 JS 对象

javascript - Jquery .get 未检索文件

将十进制年值转换为年、月和日的Javascript函数

php - Node js/Express 替代 LAMP

node.js - 如何在 Node.js 中实现 "Base64 encoded SHA-1 hash of a string"

node.js - Connect-flash 消息不适用于 res.render ("SomeView")