Angular Routing:Call 来自不同路由器链接的同一模块(或组件)上的不同功能

Angular Routing:Call different function on same module(or component) from different router links

我有一个名为 Items 的模块,我导航到它的 items.Component.ts 从许多路由器 links.To 跟踪我们来自哪个页面的 id 可以与路由器路径一起传递。

this.router.navigate(['/items',id passed along with);

但我想根据我之前来自的页面调用不同的函数 ngOnit(). 前任。如果我从菜单导航,我想在 itemscomponent 中调用函数 A 或者 B 如果我从类别

导航

还可以从 模块(或组件)html 文件 或 component.ts 完成所需的导航(查看模型) 我怎样才能在 angular 中做到这一点?

你可以使用 queryParams 并从菜单页面发送 1,从类别发送 2,导航后你可以执行与你查询参数相关的功能

   this.router.navigate(['/items'],id, { queryParams: { functionToexecute: 1  } });

并在您的 itemsComponent 中:

 constructor(
   private route: ActivatedRoute,
   private router: Router) {}

  ngOnInit() {
   this.route
    .queryParams
    .subscribe(params => {
     if (params['functionToexecute'] === 1){ call functionA} else{call 
      function B}
   });
 }