我的App目前工作正常,除了我的路由器让我回到默认路由.For example:I am on localhost:4000Settingsthan refresh:and I land on localhost:4000That happens on all sites.
有人知道为什么会发生这种情况吗? 顺便说一下,我没有设置默认路由,虽然。
这是我的路由模块。
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { HomeComponent } from './home/home.component';
import { MediaComponent } from './media/media.component';
import { RegisterComponent } from './register/register.component';
import { AuthGuard } from './auth.guard';
import { SettingsComponent } from './settings/settings.component';
import { LoginComponent } from './login/login.component';
const routes: Routes = [
{path: '', component: LoginComponent},
{path: 'Register', component: RegisterComponent},
{path: 'Home', component: HomeComponent, canActivate:[AuthGuard]},
{path: 'Media', component: MediaComponent, canActivate:[AuthGuard]},
{path: 'Settings', component: SettingsComponent, canActivate:[AuthGuard]}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
AuthGuard:
canActivate(
route: ActivatedRouteSnapshot,
router: RouterStateSnapshot): Observable<boolean> {
return this.afAuth.authState.pipe(
take(1),
map(user => !!user),
tap(loggedIn => {
if (!loggedIn) {
console.log('Access Denied');
window.alert('kein Zugang bitte Registrieren oder einlogen')
this.router.navigate(['Login']);
}
})
);
}
Index.html。
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Luke's Page</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
<app-root></app-root>
</body>
</html>
App.Component:
<app-navbar></app-navbar>
<router-outlet></router-outlet>
尝试把你的默认路由最后
const routes: Routes = [
{path: 'Register', component: RegisterComponent},
{path: 'Home', component: HomeComponent, canActivate:[AuthGuard]},
{path: 'Media', component: MediaComponent, canActivate:[AuthGuard]},
{path: 'Settings', component: SettingsComponent, canActivate:[AuthGuard]},
{path: '', component: LoginComponent},
];
路由器会选择第一个与之匹配的。