Angular2Dart Router.currentInstruction not being set

Issue

I am writing a simple paging widget in Angular2Dart. The component has a route method that should get the current route, update parameters, and navigate. I’m not sure I am doing this correctly, but in this current implementation, I am using router.currentInstruction. The problem is router.currentInstruction is null.

Here is the component (simplified for this question):

import 'package:angular2/angular2.dart';
import 'package:angular2/router.dart';
import 'package:angular_components/angular_components.dart';

@Component(
  selector: 'paging-component',
  templateUrl: 'paging_component.html',
  styleUrls: const [
    'paging_component.css'
  ],
  directives: const [
    CORE_DIRECTIVES,
    FORM_DIRECTIVES,
    materialDirectives,
  ],
)
class PagingComponent {

  final RouteParams routeParams;
  final Router _router;

  PagingComponent(this.routeParams, this._router);

  void _route([Map<String, String> updatedParams]) {
    var existingParams = new Map.from(routeParams.params);
    if (updatedParams != null) {
      existingParams.addAll(updatedParams);
    }
    var currentRoute = _router.currentInstruction.component.routeName;
    _router.navigate([currentRoute, existingParams]);
  }
}

Solution

You can get the currentInstruction using

@override
void routerOnActivate(next, prev) {
  print('Activating ${next.routeName} ${next.urlPath}');
}

there might be other ways. I can’t tell if _router.currentInstruction in your example should work. It might depend on where route(...) is being called from.

See also https://webdev.dartlang.org/angular/api/angular2.router/OnActivate-class

Answered By – Günter Zöchbauer

Answer Checked By – Jay B. (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.