如果用户移至错误的子路径,我将尝试将其重定向。我已经为请求数据的api的子组件创建了防护,但是我不确定如果数据不存在该如何重定向用户。如果数据不存在或重定向到主页,我想为用户显示404。对不起,如果重复,但我找不到解决方案。
场景:
public login(password: string) {
firebase.database().ref("clients")
.orderByChild("password").equalTo(password).once("value", snapshot => {
if (snapshot.exists()) {
const route = Object.keys(snapshot.val())[0];
this.router.navigate(["/strefa-klienta", route]);
} else {
this._snackBar.open(
"Podano błędne hasło. Spróbuj jeszcze raz.",
"Zamknij",
{ duration: 2000 }
);
}
});
}
canActivate(
child: ActivatedRouteSnapshot,
state: RouterStateSnapshot
): Observable<boolean> | boolean {
return this.clientsState.loading$.pipe(
map(loading => {
if (!loading) {
this.clientsState.load(child.url[0].path);
}
return true;
}),
take(1)
);
}
}
这是我的应用程序路由模块
{
path: "strefa-klienta",
children: [
{
path: "",
pathMatch: "full",
redirectTo: "/"
},
{
path: ":id",
component: components.ClientSiteComponent,
canActivate: [guards.ClientGuard]
},
],
}
例如。我有2位用户[John,Rick](“ / strefa-klienta / John”)。如果有人试图找到一个不存在“ Anna”(“ / strefa-klienta / Anna”)的用户,我想显示404。
感谢您的帮助
我认为您必须将此添加到您的app-routing.module中
{path:'',redirectTo:'home',pathMatch:'full'},{path:'**',redirectTo:'home',pathMatch:'full'},