下面是一個示例。當我點擊'註銷'按鈕時,它將重定向到查詢參數message=loggedout
的根路徑。當我然後點擊「登錄」按鈕,並重定向到登入道路,message=loggedout
參數是仍然存在 - 即:/signin?message=loggedout
切換到新URL時,角度2查詢參數會保留
App.component.ts
import { Component, OnInit } from '@angular/core';
import { ROUTER_DIRECTIVES } from '@angular/router';
import {AuthService} from '../../services/auth.service';
import { Router } from '@angular/router';
@Component({
moduleId: module.id,
selector: 'nav',
templateUrl: 'app.component.html',
directives: [ROUTER_DIRECTIVES],
providers: [AuthService]
})
export class AppComponent implements OnInit {
isLoggedIn = this._auth.isLoggedIn.check;
constructor(private _auth: AuthService, private _router: Router) {}
ngOnInit() {
}
logout() {
this._auth.logout()
.then(() => {
this._router.navigateByUrl('/?message=loggedout');
}
);
}
}
應用.component.html
<nav>
<div class="nav-wrapper" style='background-color: #226784'>
<a class="brand-logo"[routerLink]="['/']">Site</a>
<ul class="right hide-on-med-and-down">
<li *ngIf="!isLoggedIn()"><a [routerLink]="['signin']" [queryParams]="">Sign In</a></li>
<li *ngIf="!isLoggedIn()"><a [routerLink]="['signup']" [queryParams]="">Sign Up</a></li>
<li *ngIf="isLoggedIn()"><a (click)="logout()">Logout</a></li>
</ul>
</div>
</nav>
<div class="container">
<router-outlet></router-outlet>
</div>