【问题标题】:refresh data on the activated route - reroute to it刷新激活路由上的数据 - 重新路由到它
【发布时间】:2019-08-22 19:05:33
【问题描述】:

我想通过再次路由到同一个路由器来刷新数据。当有第二个(或更多)路由到当前激活的路由时,Angular 不会激活 ngOnInit 组件功能。我想在路由时启用刷新组件上的数据(即使它已经是激活的路由)。我知道这样做的技术很少,但我想使用 RouterEvent 和
NavigationEnd 技术。我知道我需要在 路由模块。 由于某种原因,我无法达到我想要的场景。 查看我的代码: 这是单击按钮时触发的事件(单击按钮两次应该路由两次到同一个组件):

openMapTool(item: MapToolsItem)
{
   this.router.navigate([{ outlets: { <Name of router outlet>: ['SomePath', { param1: value1}] }}],);
  return false;
}

这是与“SomePath”路径相关的组件(见下文 - “SomeComponent”) - 应该路由两次的组件:

 ngOnInit() {
this.subs.push(this.route.events.pipe(
  filter(e => e instanceof RouterEvent && e instanceof NavigationEnd),
).subscribe(e => 
  {
    this.doSomething(e.paramMap);
  }));

这是包含“SomePath”路径的模块:

const routes: Routes = [

{ path: 'SomePath', component: SomeComponent, outlet: 'SomeRouterOutlet'}];

@NgModule({
imports: [
RouterModule.forChild(routes),
RouterModule.forRoot(routes, {onSameUrlNavigation:'reload'})]}) 

感谢您的帮助

【问题讨论】:

    标签: angular angular-routing


    【解决方案1】:

    看起来您的组件已被缓存然后重新使用。检查您的 app.module.ts 中是否定义了 RouteReuseStrategy。如果是这种情况,您应该以编程方式从 RouteReuseStrategy 类的缓存中清除组件。

    app.module.ts
    
    providers: [
        { provide: RouteReuseStrategy, useClass: <class name>}
    

    要访问app.module.ts 中定义的类,请使用Router.routeReuseStrategy

    【讨论】:

      【解决方案2】:

      您可以尝试订阅 routePrams 并采取相应措施。

      this.route.params.subscribe(params =>
        // Do something
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-10-28
        • 1970-01-01
        • 2017-07-27
        • 2020-06-20
        • 1970-01-01
        • 1970-01-01
        • 2018-12-06
        • 2017-02-01
        相关资源
        最近更新 更多