动态登录/注销导航栏



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

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

在我的索引中:

`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 ='';

阿拉伯数字。 app.get('/', (request, response( => {

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

    如果经过身份验证 p 欢迎 #{用户名}!

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

最新更新