额外/重定向默认路由时

时间:2014-01-28 22:52:09

标签: dart angular-dart

我将RouteInitializer配置如下:

class AppRouteInitializer implements RouteInitializer {

  init(Router router, ViewFactory view) {
    router.root
      ..addRoute(
          name: 'root',
          path: '/lounge_client_demo/web/lounge_client_demo.html',
          mount: (Route route) => route
            ..addRoute(
                name: 'lobby',
                path: '#lobby',
                enter: view('views/LobbyView.html'))
            ..addRoute(
                name: 'chat',
                path: '#chat',
                enter: view('views/ChatView.html'))
            ..addRoute(
                name: 'default',
                defaultRoute: true,
                enter: (_) => router.go('lobby', {'param': ':param'}, startingFrom: route, replace: false))
      );
  }

}

用于游说和聊天的网址:

http://127.0.0.1:3030/lounge_client_demo/web/lounge_client_demo.html#lobby http://127.0.0.1:3030/lounge_client_demo/web/lounge_client_demo.html#chat

按预期工作。但是,当我提交的网址如下:

http://127.0.0.1:3030/lounge_client_demo/web/lounge_client_demo.html

http://127.0.0.1:3030/lounge_client_demo/web/lounge_client_demo.html#xxx

defaultRoute中的规则产生了以下错误的网址:

http://127.0.0.1:3030/lounge_client_demo/web/lounge_client_demo/.html#lobby

我在这里做错了还是只是一个错误?

更新:日志记录显示网址中的点已被转义,可能会被错误地转换为目标网址。

2014-01-29 01:20:18.026: FINEST : route /lounge_client_demo/web/lounge_client_demo.html [Route: null]
2014-01-29 01:20:18.035: FINEST : _processNewRoute /lounge_client_demo/web/lounge_client_demo.html
2014-01-29 01:20:18.047: FINEST : route  [Route: root]
2014-01-29 01:20:18.055: FINEST : _processNewRoute 
2014-01-29 01:20:18.066: FINEST : go /lounge_client_demo/web/lounge_client_demo\.html#lobby
2014-01-29 01:20:18.075: FINEST : route #lobby [Route: root]
2014-01-29 01:20:18.087: FINEST : _processNewRoute #lobby
2014-01-29 01:20:18.099: FINEST : route  [Route: default]
2014-01-29 01:20:18.108: FINEST : route  [Route: lobby]

更新:我已将lounge_client_demo.html复制到test.file.with.dots.html并获取以下网址重写:

http://127.0.0.1:3030/lounge_client_demo/web/test/.file/.with/.dots/.html#lobby

日志记录显示每个句点都被反斜杠“转义”:

2014-01-30 01:16:28.246: FINEST : go /lounge_client_demo/web/test\.file\.with\.dots\.html#lobby

可能是route_hierarchical包中句点字符的问题?!​​

1 个答案:

答案 0 :(得分:1)

您不应该在路径路径中包含#,因此只有'lobby',而不是'#lobby' - 路由器正在为您执行此操作。如果要使用片段(#...)进行路由,则需要在模块中包含以下内容:

module.value(NgRoutingUsePushState, NgRoutingUsePushState.value(false));

告诉路由器使用window.location.hash代替window.location.path进行路由,以及收听window.onHashChange而不是window.onPopState

也就是说,您可以进行路径匹配或片段匹配,但不能同时进行。因此,您还需要删除“根”路径(路径'/lounge_client_demo/web/lounge_client_demo.html')。一旦切换到片段匹配路径变得无关紧要,只有#之后的东西传递给路由器。

您的路线配置应如下所示:

class AppRouteInitializer implements RouteInitializer {

  init(Router router, ViewFactory view) {
    router.root
        ..addRoute(
            name: 'lobby',
            path: 'lobby',
            enter: view('views/LobbyView.html'))
        ..addRoute(
            name: 'chat',
            path: 'chat',
            enter: view('views/ChatView.html'))
        ..addRoute(
            name: 'default',
            defaultRoute: true,
            enter: (_) => router.go('lobby', {'param': ':param'}));
  }

}