我面臨着以下問題:其通過AuthGuard服務保護個人節,當我瀏覽到它或者通過瀏覽器刷新加載兩次。第二次,如果我提供任何URL,它將刪除URL的查詢參數。這裏是我的應用程序路由器的配置:懶加載的子路徑負載兩次角
const routes: Routes = [
{
path: 'search',
redirectTo: '',
pathMatch: 'full'
},
{
path: '',
component: BookmarksComponent
},
{
path: 'tagged/:tag',
component: TagComponent
},
{
path: 'about',
component: AboutComponent
},
{
path: 'personal',
loadChildren: 'app/personal/personal-bookmarks.module#PersonalBookmarksModule'
}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule {}
和孩子路由器配置
@NgModule({
imports: [RouterModule.forChild([
{
path: '',
component: PersonalBookmarksComponent,
canActivate: [AuthGuard],
children: [
{
path: '',
component: PersonalBookmarksListComponent
},
{
path: 'new',
component: NewPersonalBookmarkFormComponent
},
{
path: 'bookmarks/:id',
component: PersonalBookmarkDetailComponent
}
]
}
])],
exports: [RouterModule]
})
export class PersonalBookmarksRoutingModule {}
的AuthGuard服務(由此,如果它只返回true是相同的行爲):
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private keycloakService: KeycloakService) {}
canActivate() {
console.log('AuthGuard#canActivate called');
if (this.keycloakService.isLoggedIn()) {
return true;
} else {
this.keycloakService.login();
}
}
}
而且在導航欄模板:
<nav class="navbar navbar-light bg-faded" id="navbar">
<a class="navbar-brand" [routerLink]="['']" routerLinkActive="active">
<img src="assets/logo.png" width="35" height="35" class="d-inline-block align-top" alt="">
Public Bookmarks
</a>
<ul class="nav navbar-nav">
<li class="nav-item">
<a class="nav-link" [routerLink]="['personal']" routerLinkActive="active">Personal list</a>
</li>
<li class="nav-item">
<a class="nav-link" [routerLink]="['about']" routerLinkActive="active">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="http://www.codingpedia.org/tags/#codingmarks" target="_blank">Blog</a>
</li>
<li *ngIf="isLoggedIn else notLoggedIn" class="nav-item">
<a class="nav-link" (click)="logout()">Logout <i class="fa fa-lock"></i></a>
</li>
<ng-template #notLoggedIn>
<li *ngIf="!keycloakService.isLoggedIn()" class="nav-item">
<a class="nav-link" (click)="login()">Login <i class="fa fa-unlock"></i></a>
</li>
</ng-template>
</ul>
</nav>
該項目可 on Github和錯誤行爲可以通過https://www.codingmarks.org/personal與登錄測試的用戶名/密碼 - [email protected]/Test_user1$
UPDATE 即使我刪除PersonalBookmarksComponent和移動AuthGuard到PersonalBookmarks模塊錯誤的行爲仍然存在......對於PersonalBookmarksModule的路線看起來類似以下內容:
const personalBookmarksRoutes: Routes = [
{
path: 'search',
redirectTo: '',
pathMatch: 'full'
},
{
path: '',
component: PersonalBookmarksListComponent,
canActivate: [AuthGuard],
},
{
path: 'new',
component: NewPersonalBookmarkFormComponent
},
{
path: 'bookmarks/:id',
component: PersonalBookmarkDetailComponent
}
];
UPDATE2: 剝離掉的查詢參數之一是由於與Keycloak登錄時強制重定向。
假:
public login(): Promise<any> {
let options: any;
options = {redirectUri: environment.HOST + 'personal'};
return new Promise<any>((resolve, reject) => {
KeycloakService.auth.login(options)
.success(resolve)
.error(reject);
});
}
正確:
public login(): Promise<any> {
return new Promise<any>((resolve, reject) => {
KeycloakService.auth.login()
.success(resolve)
.error(reject);
});
}
您可以嘗試爲'BookmarksComponent'添加一個無空路徑 –
我嘗試過,但得到相同的行爲,爲什麼它會有所幫助? – Adrian