尝试重定向时出现无限循环

时间:2020-01-23 13:52:34

标签: javascript angular typescript

如果我访问页面 search http://localhost:4200/search),我想重定向到首页(http://localhost:4200)。

我的文件 login.guards.ts 中具有此功能:

  hasAccess() {
      return new Promise(resolve => {
        this.loginService.hasCookie(this.cookieService.get('login')).subscribe(hasCookie => {

          // User is not logged in
          console.log(this.location.path());
          if (this.location.path() === '/login' || this.location.path() === '/register' || this.location.path() === '') {
            return resolve(true);
          }
          return resolve(this.router.createUrlTree(['']));

        });
      });
    }

app-routing.modules.ts:

const routes: Routes = [
  { path: '', component: HomeComponent, canActivate: [LoginGuard] },
  { path: 'login', component: LoginComponent, canActivate: [LoginGuard] },
  { path: 'register', component: RegisterComponent, canActivate: [LoginGuard] },
  { path: 'search', component: SearchComponent, canActivate: [LoginGuard] },
  { path: 'edit-profile', component: ProfileComponent }
];

我的问题是,一旦我访问 / search 页面,就会出现无限循环,并且无法重定向到主页。我在做什么错了?

0 个答案:

没有答案