简体   繁体   English

Router.navigate 未在 Angular 中加载整个页面内容

[英]Router.navigate not loading the full page content in Angular

I am trying to fix an issue users are having after impersonating as a different user in the system.我正在尝试解决用户在系统中冒充其他用户后遇到的问题。 Currently, a regional user could impersonate as someone else which does not load the full content of the page as they now have to press F5 to see everything on this page.目前,区域用户可以冒充其他不加载页面全部内容的人,因为他们现在必须按 F5 才能查看此页面上的所有内容。 I tried to reproduce this issue by pointing to the same database in local but not able to do so.我试图通过指向本地相同的数据库来重现此问题,但无法这样做。 When I go to the DEV url for instance, I then try impersonating as a different user which then loads the page partially and refreshing the page (F5), I see the entire content.例如,当我转到 DEV url 时,我尝试冒充另一个用户,然后部分加载页面并刷新页面 (F5),我看到了整个内容。 I believe this is happening due to the route navigate, not sure if I am missing anything to pass in this function.我相信这是由于路线导航而发生的,不确定我是否遗漏了要传递此函数的任何内容。

this.router.navigate(['/', user.Company.Abbreviation.toLowerCase()]);

This is the full function that the Impersonate button is executing.这是 Impersonate 按钮正在执行的全部功能。

setImpersonatation() {
this.store.dispatch(new AuditSearchClearAction());
this.store.dispatch(new CompanyChangedAction(this.impersonationForm.value.company, null));

const user = this.impersonationForm.value.user as UserLogin;
this.store.dispatch(new BaseUserSetAction(user as User));
this.store.dispatch(new AuthorizeAction(user));
this.router.navigate(['/', user.Company.Abbreviation.toLowerCase()]);
this.store.dispatch(new MyWorkSetLetterReminderUserAction(user.UserId));

} }

When I switch a user, I get the screen below.当我切换用户时,我得到下面的屏幕。 在此处输入图像描述

But when I refresh the page (F5), then i see the entire data.但是当我刷新页面 (F5) 时,我会看到整个数据。 在此处输入图像描述

Do I need to add any parameters to the router.navigate so it loads the page correctly?我是否需要向 router.navigate 添加任何参数以便正确加载页面? Seems like something is missing of when trying to load the page after the Impersonate button is clicked.单击“模拟”按钮后尝试加载页面时似乎缺少某些内容。 Thanks!谢谢!

UPDATE:更新:

setImpersonatation() {
this.store.dispatch(new AuditSearchClearAction());
this.store.dispatch(new CompanyChangedAction(this.impersonationForm.value.company, null));

const user = this.impersonationForm.value.user as UserLogin;
this.store.dispatch(new BaseUserSetAction(user as User));
this.store.dispatch(new AuthorizeAction(user));
this.store.dispatch(new MyWorkSetLetterReminderUserAction(user.UserId));
**this.changeDetector.detectChanges();
this.router.navigate(['/', user.Company.Abbreviation.toLowerCase()]);**

} }

Using a resolver is always a good choice in cases that data is not ready to generate the page UI.在数据尚未准备好生成页面 UI 的情况下,使用解析器始终是一个不错的选择。 Resolver is preventing your app from navigate unless the data is fetched completely from server.除非数据完全从服务器获取,否则解析器会阻止您的应用导航。

Steps:脚步:

  1. create a resolver service by running this command:通过运行以下命令创建解析器服务:
ng generate resolver test-resolver
  1. you have to put your ajax call in the resolve method of resolver:你必须把你的ajax调用放在resolver的resolve方法中:
@Injectable({ providedIn: 'root' })
export class TestResolver implements Resolve<Hero> {
  constructor(private service: TestService) {}

  resolve(
    route: ActivatedRouteSnapshot,
    state: RouterStateSnapshot
  ): Observable<any>|Promise<any>|any {
    return this.service.testAjax();
  }
}
  1. you have to define your resolver in your routing module:你必须在你的路由模块中定义你的解析器:
@NgModule({
  imports: [
    RouterModule.forRoot([
      {
        path: 'test',
        component: TestComponent,
        resolve: {
          test: TestResolver
        }
      }
    ])
  ],
  exports: [RouterModule]
})
export class AppRoutingModule {}
  1. you have to catch the response in your component (in this case TestComponent):您必须在组件中捕获响应(在本例中为 TestComponent):
@Component({
  selector: 'app-test',
  template: ``
})
export class TestComponent {
  constructor(private activatedRoute: ActivatedRoute) {
    this.activatedRoute.data.subscribe((data) => {
      console.log(data.test);
    });
  }
}

In this way you will navigate to the next page ONLY if data is fully loaded and this feature makes it so much easier to implement a better user experience like a loading on navigation to those pages that require some data to be fetched.通过这种方式,只有在数据完全加载时,您才会导航到下一页,并且此功能可以更轻松地实现更好的用户体验,例如在导航到需要获取一些数据的页面时加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM