简体   繁体   English

Angular:变化检测

[英]Angular: Change detection

I am working on an application where I have defined numerous tabs on the navbar.我正在开发一个应用程序,我在导航栏上定义了许多选项卡。 Some of them are hidden.其中一些是隐藏的。 I have a login tab where after the user logs in I want to make it disappear and display the log out tab.我有一个登录选项卡,在用户登录后我想让它消失并显示注销选项卡。 I have correctly made my logic but, I am having an issue.我已经正确地提出了我的逻辑,但是我遇到了问题。 All those tabs are routes whereas navbar component isn't.所有这些选项卡都是路由,而导航栏组件不是。 It only gets rendered one time as I start the application.当我启动应用程序时,它只会呈现一次。 I have used service in my login component and passing that data to my navbar component but, despite after the user logs in, the navbar isn't updating with new tab (log out) on its own.我在我的登录组件中使用了服务并将该数据传递给我的导航栏组件,但是,尽管用户登录后,导航栏并没有自行更新新选项卡(注销)。 I have to let's say click on a button to call the ngOnInit() method of navbar component again.我不得不说单击一个按钮以再次调用导航栏组件的 ngOnInit() 方法。 Here's my code:这是我的代码:

navbar.component.html navbar.component.html

<div class="collapse navbar-collapse" id="navbarSupportedContent" >
<ul class="navbar-nav ml-auto">
  <li class="nav-item" routerLinkActive = "active" [routerLinkActiveOptions] = "{exact: true}">
    <a class="nav-link" routerLink = "/">Home</a>
  </li>
  <li class="nav-item" routerLinkActive = "active">
    <a class="nav-link" routerLink = "/findcourse">Find Courses</a>
  </li>
  <li class="nav-item" routerLinkActive = "active">
    <a class="nav-link" routerLink = "/feedback">Feedback</a>
  </li>
  <li class="nav-item" routerLinkActive = "active">
    <a class="nav-link" routerLink = "/register">Register</a>
  </li>
  <li class="nav-item" routerLinkActive = "active" *ngIf = "loggedIn">
    <a class="nav-link" routerLink = "/login">Login</a>
  </li>
  <li class="nav-item" routerLinkActive = "active" *ngIf = "loggedOut">
    <a class="nav-link" routerLink = "/logout">Log out</a>
  </li>
  <button type="button" name="button" class = "btn btn-outline-info" (click) = "ngOnInit()">Check</button>
</ul>
</div>

navbar.component.ts navbar.component.ts

ngOnInit(): void         //I want to call this on its own not only at the time of rendering but every time user logs in
{
if(this.login.isAuthenticated() == true)
{
  this.loggedOut = true;
  this.loggedIn = false;
}
else if(this.login.isAuthenticated() == false)
{
  this.loggedOut = false;
  this.loggedIn = true;
}
}

login.component.ts登录组件.ts

logIn(form: NgForm)
{
 this.http.post('http://localhost:3000/login', form.value, {responseType: "text"})
 .subscribe(responseData => {
  if(responseData == 'Successfully logged in')
  {
    this.logInSuccess = true;
    this.showlogInAlert();
    this.auth.checkAuthentication(true);
  }
  else if(responseData == 'User login failed')
  {
    this.logInFailure = true;
    this.auth.checkAuthentication(false);
  }
 }
,error =>{
this.serverError = true;
this.showserverAlert();
});

form.reset();
}

login.service.ts登录服务.ts

 export class Login
{
 logInSuccess: boolean = false;

 checkAuthentication(check : boolean)
{
 if(check === true)
 {
  this.logInSuccess = true;
 }
 else
 {
  this.logInSuccess = false;
 }
}

isAuthenticated()
{
 return this.logInSuccess;
}
}

How can I re-render the navbar on its own?如何自行重新渲染导航栏? I don't know how to do that?我不知道该怎么做? Is there a way to make Angular to check for change detection in navbar on its own?有没有办法让 Angular 自行检查导航栏中的更改检测?

Quick way to fix would be to use the login service isAuthenticated value directly in your html:快速修复方法是直接在 html 中使用登录服务 isAuthenticated 值:

  <li class="nav-item" routerLinkActive = "active" *ngIf = "!login.isAuthenticated()">
    <a class="nav-link" routerLink = "/login">Login</a>
  </li>
  <li class="nav-item" routerLinkActive = "active" *ngIf = "login.isAuthenticated()">
    <a class="nav-link" routerLink = "/logout">Log out</a>
  </li>

It will get the latest value from the service as it is updated.它将在更新时从服务中获取最新值。

More efficient in terms of change detection would be to expose the value as an observable and subscribe to changes in your component.在更改检测方面更有效的是将值公开为可观察的并订阅组件中的更改。 Example in answer to this question .回答这个问题的例子。

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

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