Я новичок в Angular и Node JS.Я пытаюсь простое изменение панели навигации в зависимости от состояния ngIf.Тем не менее, это не похоже на работу.
Я уже пробовал использовать статические переменные-члены.
Кроме того, попытался создать метод, который изменяет значение isUserAuthenticated
в navbar.component
и вызывает методы как в homepage.component
, так и в dashboard.component
.
Я также сделал console.log()
и проверил значение переменной isUserAuthenticated
.Он обновляется как для homepage.component
, так и для dashboard.component
.Тем не менее, NavBar всегда остается неизменным.
Я изменил значение непосредственно в navbar.component, и тогда он работает.Итак, я брожу, почему это не работает, если я изменяю значение из других компонентов.
navbar.component.html
<div *ngIf="!isUserAuthenticated">
<li class="nav-item">
<a class="nav-link" href="/"> EX FALSE </a>
</li>
</div>
<div *ngIf="isUserAuthenticated">
<li class="nav-item">
<a class="nav-link" href="/"> EX TRUE </a>
</li>
</div>
navbar.component.ts
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-navbar',
templateUrl: './navbar.component.html',
styleUrls: ['./navbar.component.css']
})
export class NavbarComponent implements OnInit {
isUserAuthenticated = true;
constructor() { }
ngOnInit() { }
public authenticate() {
this.isUserAuthenticated = false;
return this.isUserAuthenticated;
}
public deauthenticate() {
this.isUserAuthenticated = true;
return this.isUserAuthenticated;
}
}
homepage.component.ts
import { Component, OnInit } from '@angular/core';
import { NavbarComponent } from '../navbar/navbar.component';
@Component({
selector: 'app-homepage',
templateUrl: './homepage.component.html',
styleUrls: ['./homepage.component.css']
})
export class HomepageComponent implements OnInit {
constructor( ) {
}
ngOnInit() {
console.log("here");
this.deauthenticate();
}
public deauthenticate()
{
const comp2 = new NavbarComponent();
comp2.deauthenticate();
console.log(comp2.deauthenticate());
}
}
dashboard.component.ts
import { Component, OnInit } from '@angular/core';
import { NavbarComponent } from '../navbar/navbar.component';
@Component({
selector: 'app-dashboard',
templateUrl: './dashboard.component.html',
styleUrls: ['./dashboard.component.css']
})
export class DashboardComponent implements OnInit {
constructor( ) {
}
ngOnInit() {
this.authenticate();
}
public authenticate()
{
const comp2 = new NavbarComponent();
comp2.authenticate();
console.log(comp2.authenticate());
}
}
Я хочу, чтобы компонент dashboard
отображал "EX TRUE" в NavBar и компонент homepage
показывает «EX FALSE» в NavBar.