我有一个导航栏,其中包含必须选择的不同组件。配置文件组件确实打开,但是“我的最爱”按钮未显示该组件的任何内容。该组件也位于app.module.ts中。
这是导航栏中的代码:
<form class="form-inline my-2 my-lg-0">
<ul class="nav navbar-nav navbar-right">
<li *ngIf="authService.loggedIn()" [routerLinkActive]="['active']" [routerLinkActiveOptions] = "{exact:true}"><a class="nav-link" [routerLink]= "['/favorites']">My favorites</a></li>
<li *ngIf="authService.loggedIn()" [routerLinkActive]="['active']" [routerLinkActiveOptions] = "{exact:true}"><a class="nav-link" [routerLink]= "['/dashboard']">Dashboard</a> </li>
<li *ngIf="authService.loggedIn()" [routerLinkActive]="['active']" [routerLinkActiveOptions] = "{exact:true}"><a class="nav-link" [routerLink]= "['/profile']">Profile</a></li>
<li *ngIf="!authService.loggedIn()" [routerLinkActive]="['active']" [routerLinkActiveOptions] = "{exact:true}"><a class="nav-link" [routerLink]= "['/login']">Login</a></li>
<li *ngIf="!authService.loggedIn()" [routerLinkActive]="['active']" [routerLinkActiveOptions] = "{exact:true}"> <a class="nav-link"[routerLink]= "['/register']">Register</a></li>
<li><a class="nav-link" (click)="onLogOutClick()" href="#">Logout</a></li>
</ul>
</form>
我的app.module.ts中有以下路线:
const appRoutes: Routes = [
{path: '', component: HomeComponent},
{path: 'register', component: RegisterComponent},
{path: 'login', component: LoginComponent},
{path: 'dashboard', component: DashboardComponent, canActivate: [AuthGuard]},
{path: 'profile', component: ProfileComponent, canActivate: [AuthGuard]},
{path: 'favorites', component: FavoritesComponent, canActivate: [AuthGuard]}
]
favorites.component.ts:
import { Component, OnInit } from '@angular/core';
import { AuthService } from 'src/app/services/auth.service';
import { Router } from '@angular/router';
@Component({
selector: 'app-favorites',
templateUrl: './favorites.component.html',
styleUrls: ['./favorites.component.css']
})
export class FavoritesComponent implements OnInit {
constructor(private authService: AuthService, private router: Router) { }
ngOnInit() {
}
}
如有必要,我可以为您提供更多代码。