I have this HTML
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class='container'>
<ul class="nav navbar-nav">
<li class='nav-item'>
<a class='nav-link' routerLink="/">Home</a>
</li>
<li class='nav-item'>
<a class='nav-link' routerLink="/about">About</a>
</li>
<li class='nav-item'>
<a class='nav-link' routerLink="/login">Log in</a>
</li>
<li class='nav-item'>
<a class='nav-link' routerLink="/register">Register</a>
</li>
<li class='nav-item'>
<a class='nav-link' (click)="doLogout()" routerLink="/logout">Log out</a>
</li>
</ul>
</div>
</nav>
In an auth.service.ts file I have this method:
doLogout() {
return new Promise((resolve, reject) => {
if (firebase.auth().currentUser) {
this.afAuth.auth.signOut();
localStorage.setItem('isLoggedIn', 'false');
resolve();
}
else {
reject();
}
});
}
Now how can I call this method using the (click) in Angular 5? Google is weirdly silent about Angular 5. Right now, if I click the Log out button I get this error ERROR Error: "Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'logout'. How can I get it to do run the function, instead of the relocation? I thought there is some kind of "preventDefault" in the background.
My routes
export const rootRouterConfig: Routes = [
// {path: '', redirectTo: '', pathMatch: 'full'},
{path: '', component: HomeComponent},
{path: 'about', component: AboutComponent},
{path: 'login', component: LoginComponent, canActivate: [AuthGuard]},
{path: 'register', component: RegisterComponent, canActivate: [AuthGuard]},
{path: 'user', component: UserComponent, resolve: {data: UserResolver}},
// {path: 'tasks', component: TasksComponent, canActivate: [AuthGuard]},
];
/logoutroute, which is what the error is telling youdoLogoutfunction has nothing to do with routing at the moment. And even if you wanted to handle redirection inside the function, you'd still need to add the route to your routerConfig