首页 > 解决方案 > Angular Dart - 从父路由中检索参数

问题描述

在模板中,我有以下 routerLink: [routerLink]="['../myObjects', {'id': object.id}, 'mySubObjectsList']"

路由没问题,但我无法id在 mySubObjectsList 组件中获取参数值。_routeParams.params是空的。

我猜这是因为参数设置为我的父路由组件myObjects

有没有办法在我的子组件中获取它?

谢谢你的帮助。

标签: angulardart

解决方案


我不认为有一个简单的方法。您可以将参数添加到父服务中,并使该服务可供子服务使用:

@Service()
class ParentRouteParams {
  Map<String,String> params;
  // using a StreamController is probably a good idea
}

@Component(
  selector: 'parent',
  providers: const [ParentRouteParams],
  ...
)
class Parent {
  Parent(this._parentRouteParams);

  final ParentRouteParams _parentRouteParams;

  @override
  void onActivate(_, RouterState current) {
    _parentRouteParams.params = current.parameters;
  }
}
@Component(
  selector: 'child'
  ...
)
class Child {
  Parent(this._parentRouteParams);

  final ParentRouteParams _parentRouteParams;

}

推荐阅读