首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Angular Lazy加载模块重新加载不起作用

Angular Lazy加载模块重新加载不起作用
EN

Stack Overflow用户
提问于 2020-05-05 23:37:24
回答 1查看 939关注 0票数 0

我已经实现了延迟加载模块,并且在模块的路由中加载子组件,

就像这样。

代码语言:javascript
复制
  const routes: Routes = [
  {
    path: "",
    component: CustomerListComponent,
    children: [
      {
        path: "child",
        component: CustomerChildComponent
      }
    ]
  }
];

在CustomerListComponent的OnInit中,我导航到子组件,如下所示

代码语言:javascript
复制
export class CustomerListComponent implements OnInit {
constructor(private router: Router) {}
  ngOnInit() {
    this.router.navigate(["customers/child"]);
  }
}

我的问题是,如果我单击Menu/Button,它将重定向/加载到此模块,第一次工作正常,并且一旦加载了此模块,如果再次单击相同的菜单/按钮,它不会像这样重新加载/重定向到子组件,而是加载为“/customers

所以我的问题是,第一次我可以看到Customers和Child两个组件,但在下一次单击时,我只能看到Customers组件。

在我的实时场景中,我在customer组件中保留了一个选项卡和一个网格,该网格应该对所有选项卡可用,

在客户组件中的router-outlet中,我加载子组件。

所以在第二次点击时,我只看到了网格和子组件。

我试着用这个Stackblitz来模拟这个问题

在stackblitz中,您可以注意到,在第二次单击Customers按钮时,该子节点将从路由中删除。

请帮帮忙

EN

回答 1

Stack Overflow用户

发布于 2020-07-16 14:48:36

我通过在构造函数中添加以下代码解决了这个问题

代码语言:javascript
复制
this.subscriptions.add(router.events.subscribe((evt: any) => {
  if (evt.url === '/customers') {
    this.router.navigateByUrl('/customers/child');
  }
}));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61617006

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档