Node - EJS - 显示 toastjs 消息

问题描述 投票:0回答:2

我正在尝试使用 ToastrJS 库显示 toast 消息。消息显示正常。问题是我没有找到传递错误消息的方法。我使用 connect-flash 来发送闪存消息,然后我想将消息传递给烤面包机。

我的EJS代码:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title><%=title%></title>
    <link rel="stylesheet" href="/css/bootstrap.min.css">
    <link rel="stylesheet" href="/stylesheets/nav-bar.css">
    <link rel="stylesheet" href="<%=style%>">
    <link rel="stylesheet" href="/js/aos.css">
    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
      <link href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/2.0.1/css/toastr.css" rel="stylesheet"/>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/2.0.1/js/toastr.js"></script>
  </head>
  <body >
    <nav class="navbar navbar-inverse navbar-fixed-top">
      <div class="container-fluid">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="#">Joobber</a>
        </div>
        <div class="collapse navbar-collapse" id="myNavbar">
          <ul class="nav navbar-nav">
            <li class=""><a href="#">Home</a></li>
            <li><a href="#">Ver todas as vagas</a></li>
            <li><a href="#">Vagas por categoria:</a></li>

          </ul>
          <ul class="nav navbar-nav navbar-right">
            <li><a href="#"><span class="glyphicon glyphicon-user"></span> Ainda não está cadastrado?</a></li>
            <li><a href="#"><span class="glyphicon glyphicon-log-in"></span> Logar</a></li>
          </ul>
        </div>
      </div>
    </nav>

    <% if(success.length > 0){ %>
      <script>
        toastr.success('WANNA USE THE ERROR MESSAGE VARIABLE HERE!!!!')
      </script>
    <% } %>

    <script type="text/javascript" src="js/aos.js">

    </script>
    <script type="text/javascript">
    AOS.init({
      offset: 200,
      duration: 600,
      easing: 'ease-in-sine',
      delay: 100,
      });
    </script>

我还配置了 connect-flash 并使用一些本地变量来设置特定类型的消息

app.use(flash());
var sessionFlash = function(req, res, next) {
    res.locals.currentUser = req.user;
    res.locals.error = req.flash('error');
    res.locals.success = req.flash('success');
    next();

}
app.use(sessionFlash)

然后,我的路线:

const express = require('express')
const router = express.Router()
const indexControllers = require('../controllers/indexControllers')


router.get('/', indexControllers.HomePage)

router.get('/signup', indexControllers.Signup)

router.get('/test', (req, res)=>{
  req.flash('success', 'user succesfulyl registered')
  res.redirect('/')
})

module.exports = router;
javascript node.js express ejs flash-message
2个回答
0
投票

如果你想用一句话显示带有错误和消息的toastr,那么使用toastr.info。如果没有,最好将成功消息和错误消息分开以获得更清晰的上下文。并像这样使用它:

<% if(success.length > 0){ %>
  <script>
    toastr.success(`${success[0]}`)
  </script>
<% } %>
<% if(error.length > 0){ %>
  <script>
    toastr.error(`${error[0]}`)
  </script>
<% } %>

0
投票

我已经尝试过你的方法,但没有按预期工作

<script>
    '<% if (adminCountError.length>0) { %>'
         toastr.error("<%= adminCountError %>");
     '<% } %>'
</script>

这对我有用

© www.soinside.com 2019 - 2024. All rights reserved.