如何检查子路径是否存在-角度

问题描述 投票:0回答:1

如果用户移至错误的子路径,我将尝试将其重定向。我已经为请求数据的api的子组件创建了防护,但是我不确定如果数据不存在该如何重定向用户。如果数据不存在或重定向到主页,我想为用户显示404。对不起,如果重复,但我找不到解决方案。

场景:

  1. 用户必须输入密码才能重定向到他的电影。 (Auth.Service)
 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 }
          );
        }
      });
  }
  1. 如果通过,则用户将重定向到他的路由页面,并且需要保护的数据请求(保护)
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。

感谢您的帮助

angular routing
1个回答
0
投票

我认为您必须将此添加到您的app-routing.module中

{path:'',redirectTo:'home',pathMatch:'full'},{path:'**',redirectTo:'home',pathMatch:'full'},

© www.soinside.com 2019 - 2024. All rights reserved.