|
1 | 1 | import { Component } from '@angular/core';
|
| 2 | +import { NavigationEnd, Router } from '@angular/router'; |
| 3 | +import { filter } from 'rxjs/operators'; |
| 4 | +import { AuthService } from './core/auth/auth.service'; |
2 | 5 |
|
3 | 6 | @Component({
|
4 | 7 | selector: 'app-root',
|
5 | 8 | template: `
|
6 |
| - <!--The content below is only a placeholder and can be replaced.--> |
7 |
| - <div style="text-align:center" class="content"> |
8 |
| - <h1> |
9 |
| - Welcome to {{title}}! |
10 |
| - </h1> |
11 |
| - <span style="display: block">{{ title }} app is running!</span> |
12 |
| - <img width="300" alt="Angular Logo" src="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAyNTAgMjUwIj4KICAgIDxwYXRoIGZpbGw9IiNERDAwMzEiIGQ9Ik0xMjUgMzBMMzEuOSA2My4ybDE0LjIgMTIzLjFMMTI1IDIzMGw3OC45LTQzLjcgMTQuMi0xMjMuMXoiIC8+CiAgICA8cGF0aCBmaWxsPSIjQzMwMDJGIiBkPSJNMTI1IDMwdjIyLjItLjFWMjMwbDc4LjktNDMuNyAxNC4yLTEyMy4xTDEyNSAzMHoiIC8+CiAgICA8cGF0aCAgZmlsbD0iI0ZGRkZGRiIgZD0iTTEyNSA1Mi4xTDY2LjggMTgyLjZoMjEuN2wxMS43LTI5LjJoNDkuNGwxMS43IDI5LjJIMTgzTDEyNSA1Mi4xem0xNyA4My4zaC0zNGwxNy00MC45IDE3IDQwLjl6IiAvPgogIDwvc3ZnPg=="> |
13 |
| - </div> |
14 |
| - <h2>Here are some links to help you start: </h2> |
15 |
| - <ul> |
16 |
| - <li> |
17 |
| - <h2><a target="_blank" rel="noopener" href="https://angular.io/tutorial">Tour of Heroes</a></h2> |
18 |
| - </li> |
19 |
| - <li> |
20 |
| - <h2><a target="_blank" rel="noopener" href="https://angular.io/cli">CLI Documentation</a></h2> |
21 |
| - </li> |
22 |
| - <li> |
23 |
| - <h2><a target="_blank" rel="noopener" href="https://blog.angular.io/">Angular blog</a></h2> |
24 |
| - </li> |
25 |
| - </ul> |
| 9 | + <button |
| 10 | + routerLink='/admin' |
| 11 | + routerLinkActive='active' |
| 12 | + *ngIf='(authService.isLogged$ | async)' |
| 13 | + > |
| 14 | + Admin (Protected) |
| 15 | + </button> |
| 16 | +
|
| 17 | + <button routerLink='/home' routerLinkActive='active'>HOME</button> |
| 18 | +
|
| 19 | + <button |
| 20 | + routerLink='/' |
| 21 | + *ngIf='!(authService.isLogged$ | async)' |
| 22 | + [routerLinkActiveOptions]='{ exact: true }' |
| 23 | + routerLinkActive='active' |
| 24 | + > |
| 25 | + Login |
| 26 | + </button> |
| 27 | +
|
| 28 | + <button (click)='logout()' *appIfLogged>LOGOUT</button> |
| 29 | +
|
| 30 | + <hr /> |
26 | 31 | <router-outlet></router-outlet>
|
27 | 32 | `,
|
28 |
| - styles: [] |
| 33 | + styles: [ |
| 34 | + ` |
| 35 | + .active { |
| 36 | + background: orange; |
| 37 | + } |
| 38 | + ` |
| 39 | + ] |
29 | 40 | })
|
30 | 41 | export class AppComponent {
|
31 |
| - title = 'angular-rxjs-real-world'; |
| 42 | + constructor(private router: Router, public authService: AuthService) { |
| 43 | + this.router.events |
| 44 | + .pipe(filter(event => event instanceof NavigationEnd)) |
| 45 | + .subscribe(res => console.log(res)); |
| 46 | + } |
| 47 | + |
| 48 | + logout(): void { |
| 49 | + this.authService.logout(); |
| 50 | + this.router.navigateByUrl('/'); |
| 51 | + } |
32 | 52 | }
|
0 commit comments