AngularDart [routerLink]不附加父路径

时间:2018-05-15 14:11:02

标签: angular dart angular-dart routerlink

我一直在AngularDart 5中构建一些简单的子路由。 该应用程序包含以下组件:

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

通过将适当的URL插入到地址栏中导航到这些组件工作正常,但使用HTML Anchor导航到它们并且[routerLink]指令不起作用,因为[routerLink]没有为子组件的URL添加前缀父组件的路径(/portal)。

我在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>

子route_paths文件:

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
);

子路线文件:

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
  ];
}

他们在浏览器中的链接如下所示:

  • localhost:8080/#/login代替localhost:8080/#/portal/login
  • localhost:8080/#/register代替localhost:8080/#/portal/register

我按照AngularDart 5路由指南来实现: https://webdev-dartlang-org-dev.firebaseapp.com/angular/guide/router/1#appcomponent-navigation

修改: 看一下RouteDefinition和RoutePath源代码..

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();
  }

使用RouteDefinition toUrl()将永远不会工作,只是RoutePath toUrl()正在合并父路径。

0 个答案:

没有答案