footer.component.html
<footer class="mt-5" *ngIf= "(user$ | async) as user">
<div class="fixed-bottom bg-white p-1">
<div class="container">
<div class="row text-center">
<div class="col-6">
<a [routerLink]="['']">
<i class="fa fa-home fa-2x"></i>
</a>
</div>
<div class="col-6">
<a [routerLink]="['p', 'add']">
<i class="fa fa-plus-circle fa-2x"></i>
</a>
</div>
</div>
</div>
</div>
</footer>
footer.component.ts
import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs';
import { UserTokenService } from '../user-token/user-token.service';
import { UserToken } from '../user-token/user-token';
@Component({
selector: 'ap-footer',
templateUrl: './footer.component.html',
styleUrls: ['./footer.component.css']
})
export class FooterComponent implements OnInit {
user$: Observable<UserToken>;
constructor(private userTokenService: UserTokenService) { }
ngOnInit(): void {
this.user$ = this.userTokenService.getUser();
}
}
user-token.service.ts
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
import { TokenService } from '../token/token.service';
import { UserToken } from './user-token';
import * as jwt_decode from 'jwt-decode';
@Injectable({
providedIn: "root"
})
export class UserTokenService {
private userSubject = new BehaviorSubject<UserToken>(null);
private userName: string;
constructor(private tokenService: TokenService){
this.tokenService.hasToken() && this.decodeAndNotify();
}
setToken(token: string){
this.tokenService.setToken(token);
this.decodeAndNotify();
}
getUser(){
return this.userSubject.asObservable();
}
private decodeAndNotify() {
const token = this.tokenService.getToken();
const decodedToken = jwt_decode(token) as UserToken;
this.userName = decodedToken.name;
this.userSubject.next(decodedToken);
}
logout() {
this.tokenService.removeToken();
this.userSubject.next(null);
}
isLogged(){
return this.tokenService.hasToken();
}
getUserName(){ //retorna o nome do usuário.
return this.userName;
}
}
app.routing.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { PhotoListComponent } from './photos/photo-list/photo-list.component';
import { PhotoFormComponent } from './photos/photo-form/photo-form.component';
import { NotFoundComponent } from './errors/not-found/not-found.component';
import { PhotoListResolver } from './photos/photo-list/photo-list.resolver';
const routes: Routes = [
{
path: '',
pathMatch: 'full',
redirectTo: 'home'
},
{
path: 'home',
loadChildren: './home/home.module#HomeModule'
},
{
path: 'user/:userName',
component: PhotoListComponent,
resolve: { fotosApi: PhotoListResolver }
},
{
path: 'p/add',
component: PhotoFormComponent
},
{
path: '**',
component: NotFoundComponent
}
];
@NgModule({
imports: [
RouterModule.forRoot(routes, { useHash: true })
],
exports: [
RouterModule
]
})
export class AppRoutingModule {
}
home.routing.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { HomeComponent } from './home.component';
import { AuthGuard } from '../core/auth/auth.guard';
import { SignInComponent } from './sign-in/sign-in.component';
import { SignUpComponent } from './sign-up/sign-up.component';
const routes: Routes = [
{
path: '',
component: HomeComponent,
canActivate: [AuthGuard],
children: [
{
path: '',
component: SignInComponent
},
{
path: 'signup',
component: SignUpComponent
},
]
}
];
@NgModule({
imports: [
RouterModule.forChild(routes)
],
exports: [
RouterModule
]
})
export class HomeRoutingModule {}