如何手动触发路由解析器 [英] How to trigger route resolver manually

查看:20
本文介绍了如何手动触发路由解析器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在访问我的用户页面帐户片段之前,我正在解析用户:

I'm resolving a user before accessing the account fragment of my user page :

app-routing.component.ts

{
  path: 'users/:id',
  component: UserComponent,
  resolve: {user: UsersService},
  children: [
    {path: '', pathMatch: 'full', redirectTo: 'account'},
    {path: 'account', component: UserAccountComponent},
    {path: 'sites', component: UserSitesComponent}
  ]
}

users.service.ts

resolve(route: ActivatedRouteSnapshot, r: RouterStateSnapshot) {
  return this.getUser(route.params['id']);
}

user-account.component.ts

ngOnInit() {
  this.route.parent.data.subscribe(data => {
    this.user = data['user'];
    // Initialize the form and its validators
    this.initForm();
  });
}

在用户帐户组件中,我可以保存或重置表单.在这两种情况下,我都想更新组件的用户并重置表单和验证器.理想情况下,它希望再次触发订阅.

In the user account component I can save or reset the form. In both cases I'd like to update the user of the component and to reset the form and the validators. Ideally It'd like the subscription to be triggered again.

知道如何手动触发解析器吗?

Any idea how to trigger the resolver manually ?

(我过去常常以随机数为模数重新导航到与最后一个片段相同的路由,但是由于我将用户页面分解为父/子路由,因此父级中的解析器在最后一次调用时不再被调用片段变化)

(I used to re-navigate to the same route modulo a random number as the last fragment, but since I exploded my user page into parent/children routes, the resolver, within the parent, is not called anymore when the last fragment changes)

推荐答案

我同意Woot",但如果你想再次触发解析器,去你的路由模块并添加

I agree with "Woot", but if you want to trigger the resolver again, go to your routing module and add

runGuardsAndResolvers: "always"

现在您可以再次在同一页面上导航,解析器将再次运行.但请注意,只有解析器会再次运行,没有其他生命周期,例如ngOnInit".

Now you can navigate on the same page again an the resolver will run again. But be aware only the resolver will run again, no other lifecyle, like "ngOnInit".

根据您的代码示例

app-routing.component.ts

{
  path: 'users/:id',
  component: UserComponent,
  resolve: {user: UsersService},
  children: [
    {path: '', pathMatch: 'full', redirectTo: 'account'},
    {path: 'account', component: UserAccountComponent, runGuardsAndResolvers: "always"},
    {path: 'sites', component: UserSitesComponent}
  ]
}

现在,如果您再次路由到同一页面,user-account.component.ts 上的订阅将再次触发

Now if you route to the same page again the subscription on your user-account.component.ts will be triggered again

这篇关于如何手动触发路由解析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆