【问题标题】:Angular 2 Change route (URL) and Load data from DBAngular 2更改路由(URL)并从数据库加载数据
【发布时间】:2018-06-04 19:16:57
【问题描述】:

在我的项目中,我有一个位于 *ngFor 循环内的菜单,并且我有 menu.Id 作为从数据库加载数据的参数

<li *ngFor="let item of menu">
  <a [routerLink]="['/products', item .Id]">{{item .Name}}</a>
</li>

当我第一次单击此菜单项(无论哪个)时,它运行良好,但之后单击另一个菜单项它不起作用,因为 ProductsComponent 已经加载并且不能再次使用 Id(路由不是' t changed '/products/:id' Id 不同,但构造函数已加载且组件已初始化)。

ProductsComponents'构造函数中获取这样的数据

 this.route.params.subscribe(params => {
      this.menuId = params.menuId;
 });
  • 如何根据路由动态加载数据?
  • 是否可以在路由中隐藏 menu.Id?

【问题讨论】:

  • 你能展示你的路由模块吗?

标签: angular routes angular2-routing


【解决方案1】:

您可以从加载的路线中获取所需的信息

例如,在您的 ngOnInit 中,您可以使用以下函数调用 getMenuId():

getMenuId() {
    const id = +this.route.snapshot.paramMap.get('menuId');
    //do something with menuId, like call another function to load your data with the id
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-31
    • 2016-07-25
    • 2023-03-22
    • 1970-01-01
    • 2017-05-14
    • 2016-06-07
    • 2016-05-11
    相关资源
    最近更新 更多