3

I have been building some simple child routes in AngularDart 5. The app contains the following components:

  • login_register_component -> localhost:8080/#/portal
  • login_component -> localhost:8080/#/portal/login
  • register_component -> localhost:8080/#/portal/register

Navigating to these components by inserting the appropiate URL into the adressbar works fine, but navigating to them using a HTML Anchor and the [routerLink] directive does not work because [routerLink] does not prefix the URL of the child component with the path of the parent component (/portal).

I have two of those anchors in login_register_component:

<nav>
    <a [routerLink]="routes.login.path">{{i18n.login}}</a>
    <a [routerLink]="routes.register.path">{{i18n.register}}</a>
</nav>
<router-outlet [routes]="routes.all"></router-outlet>

The child route_paths file:

import 'package:angular_router/angular_router.dart';

import '../route_paths.dart' as parent_route_paths;

final login = new RoutePath(
    path: 'login',
    parent: parent_route_paths.portal
);

final register = new RoutePath(
    path: 'register',
    parent: parent_route_paths.portal
);

The child routes file:

import 'package:angular_router/angular_router.dart';
import 'login_register_route_paths.dart' as paths;
import 'login/login_component.template.dart' as login_component_template;
import 'register/register_component.template.dart' as register_component_template;


class LoginRegisterRoutes {
  static final _login = new RouteDefinition(
    routePath: paths.login,
    component: login_component_template.LoginComponentNgFactory
  );

  static final _register = new RouteDefinition(
    routePath: paths.register,
    component: register_component_template.RegisterComponentNgFactory
  );

  static final _default = new RouteDefinition.redirect(
      path: '',
      redirectTo: paths.login.toUrl()
  );

  RouteDefinition get login => _login;
  RouteDefinition get register => _register;

  final List<RouteDefinition> all = [
    _login,
    _register,
    _default
  ];
}

Their link in the browser looks like this:

  • localhost:8080/#/login instead of localhost:8080/#/portal/login
  • localhost:8080/#/register instead of localhost:8080/#/portal/register

I followed the AngularDart 5 routing guide to achive this: https://webdev-dartlang-org-dev.firebaseapp.com/angular/guide/router/1#appcomponent-navigation

EDIT: Taking a look at RouteDefinition and RoutePath source code..

RouteDefinition toUrl():

String toUrl([Map<String, String> paramValues = const {}]) {
    if (isDevMode && paramValues == null) {
      throw new ArgumentError.notNull('paramValues');
    }
    var url = '/' + path;
    for (final parameter in parameters) {
      url = url.replaceFirst(
          ':$parameter', Uri.encodeComponent(paramValues[parameter]));
    }
    return url;
  }

RoutePath toUrl():

 String toUrl({
    Map<String, String> parameters,
    Map<String, String> queryParameters,
    String fragment,
  }) {
    // Don't pass parameters to parent URL. These are populated only once the
    // complete URL has been constructed.
    final parentUrl = parent != null ? parent.toUrl() : '/';
    var url = Location.joinWithSlash(parentUrl, path);
    if (parameters != null) {
      for (final key in parameters.keys) {
        url = url.replaceFirst(':$key', Uri.encodeComponent(parameters[key]));
      }
    }
    return new Url(url, queryParameters: queryParameters, fragment: fragment)
        .toString();
  }

Using RouteDefinition toUrl() wil never work, just RoutePath toUrl() is incorporating the parents path.

Tobias Marschall
  • 2,355
  • 3
  • 22
  • 40

0 Answers0