【问题标题】:Angular Routing:Call different function on same module(or component) from different router linksAngular Routing:从不同的路由器链接调用同一模块(或组件)上的不同功能
【发布时间】:2017-11-14 07:53:28
【问题描述】:

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

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

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

还可以从 both 模块(或组件)html 文件 或 component.ts(视图模型)完成所需的导航 我怎样才能在角度上实现这一点?

【问题讨论】:

    标签: angular angular4-router


    【解决方案1】:

    您可以使用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}
       });
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-27
      • 1970-01-01
      • 2014-11-01
      • 2022-01-20
      • 2019-07-19
      • 2019-12-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多