Angular 2,如何全局激活路由?

时间:2017-02-13 04:49:18

标签: angular typescript angular2-routing

如何在不涉及路由的组件中获取ActivatedRoute?

我试过这样:

constructor(
  private _route: ActivatedRoute,
) {
  this._route.params.subscribe((params: Params) => {

    console.log(params['param1'], params['param2']);

  });

}

它记录未定义,未定义。

1 个答案:

答案 0 :(得分:3)

    function findById(id) {

     //Build query
    var params = {};
    params.TableName = "recordDev";
    params.IndexName = "_id";
    params.KeyConditionExpression = '_id = :id';
    params.ExpressionAttributeValues = {
      ':id': id
    };

    return DynamoDB
        .query(params).promise()
        .then(function(records) {
          return records.Items[0];
        })
        .catch(function(error) {
          return Promise.reject(error);
        });
   };

在Angular 6中:

constructor(private router:Router) {
  router.events
  .filter(e => e instanceof NavigationEnd) 
  .forEach(e => {
    console.log(router.routerState.root);
    console.log(router.routerState.root.firstChild);
  );
}