路由器防护问题?

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

我正面临一种情况。我有一个CanActivate路由器防护装置。使用防护装置会导致整个页面不必要的重新加载。

我实施了两个版本的警卫,并弄清了一些我不理解的东西。有人可以解释这种行为并提出解决方案吗?这是路由:

const routes: Routes = [{
  path: '',
  component: DashboardComponent,
  children: [{
      path: 'overview',
      component: OverviewComponent,
    },
    {
      path: 'profile',
      canActivate: [TestGuard],
      component: ProfileComponent,
    },
  ],
}, ];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule],
})
export class MyRoutingModule {}

Guard版本1(按预期方式工作:]

import {
  Injectable
} from '@angular/core';
import {
  ActivatedRouteSnapshot,
  CanActivate,
  RouterStateSnapshot,
} from '@angular/router';
import {
  Observable,
  of
} from 'rxjs';

@Injectable({
  providedIn: 'root',
})
export class TestGuard implements CanActivate {
  constructor() {}

  canActivate(
    route: ActivatedRouteSnapshot,
    state: RouterStateSnapshot
  ): Observable < boolean > {
    return of(true);
  }
}

Guard版本2(重新加载整个页面):

import {
  Injectable
} from '@angular/core';
import {
  ActivatedRouteSnapshot,
  CanActivate,
  RouterStateSnapshot,
} from '@angular/router';
import {
  Observable,
  of
} from 'rxjs';
import {
  delay,
  map
} from 'rxjs/operators';

@Injectable({
  providedIn: 'root',
})
export class TestGuard implements CanActivate {
  constructor() {}

  canActivate(
    route: ActivatedRouteSnapshot,
    state: RouterStateSnapshot
  ): Observable < boolean > {
    return of(true).pipe(
      delay(5000),
      map(answer => answer)
    );
  }
}

我的问题是我需要调用服务(API请求)。通过第二个示例中的延迟对其进行仿真。但是在这种情况下,它将导致整个页面的重新加载。我看不到什么?

angular router guard angular-router-guards
1个回答
0
投票

最后,这不是问题。它应该工作。问题出在我们项目的某个地方。

有人可以向我解释路由器如何决定是否重新加载整个页面吗?与延迟加载模块有关吗?

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