繁体   English   中英

动态登录/注销导航栏

[英]Dynamic login/logout navbar

如果用户已登录,我希望登录按钮转换为注销。在我的index.js文件中:

app.get('/', (request, response) => {

    const authenticated = request.session.authenticated || false;
    // { user: { authenticated: authenticated } }
    response.render('index',authenticated);
    console.log(authenticated);
  });

在我的index.pug中:

`ul.nav.navbar-nav
      li.nav-item
        a.nav-link(href='foods') Foods
      if authenticated  
        li.nav-item
          a.nav-link(href='/logout') Logout
      else      
      li.nav-item.dropdown
        a#navbarDropdownMenuLink.nav-link.dropdown-toggle(href='/registration' data-toggle='dropdown' aria-haspopup='true' aria-expanded='false')
          | Register`

我没有收到任何错误,但是没有用。 我不知道该怎么解决。 也是console.log(authenticated); 显示正确的布尔值。

这就是我解决问题的方式:1。

let username ='';

2. app.get('/',(请求,响应)=> {

    const authenticated = request.session.authenticated || false;
    // { user: { authenticated: authenticated } }
    response.render('index',{ authenticated: authenticated, username: username});  
  });
  1. 在PUG中:

    如果已通过身份验证p欢迎使用{{username}!

4。

if authenticated 
    li.nav-item
      a.nav-link(href='/logout') Logout
  if !authenticated        
    li.nav-item.dropdown
      a#navbarDropdownMenuLink.nav-link.dropdown-toggle(href='/registration' data-toggle='dropdown' aria-haspopup='true' aria-expanded='false')
        | Register

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM