I think I have a solution (thanks to Ploppy), it's not perhaps THE solution but it's simple and efficient, here is my code :
The service :
import { Injectable } from '@angular/core';
@Injectable()
export class SharedService {
public isConnected: boolean;
constructor() { }
}
MenuComponent
@Component({
selector: 'app-menu',
templateUrl: './menu.component.html',
styleUrls: ['./menu.component.css']
})
export class MenuComponent implements OnInit {
@Input() isConnected: boolean;
constructor(private router: Router, private sharedService: SharedService){
this.sharedService.isConnected = false;
}
ngOnInit() {
}
logout() {
this.sharedService.isConnected = false;
this.router.navigate(['/home']);
}
}
LoginComponent
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
user: User;
isConnected: boolean = false;
constructor(private router: Router, private sharedService: SharedService) { }
ngOnInit() {
this.user = new User();
}
public login(){
if (...) {
this.partageService.isConnected=true;
this.router.navigate(['/home']);
}
}
}
AppComponent.ts
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
constructor(private router: Router, public sharedService: SharedService){}
}
AppComponent.HTML
<app-menu [isConnected]="sharedService.isConnected"></app-menu>
A little inconvenience : it is MenuComponent wich have logout() instead of LoginComponent, it's not very correct, logout() is not the responsability of MenuComponent but LoginComponent !
An idea to solve this issue ?