如何获得使用AngularDart的路由?

Joh*_*tål 6 dart angular-dart

这是我的代码,

import 'package:angular/angular.dart';

class AppModule extends Module {

  AppModule(){
    type(AppController);
    type(LoginController);
    type(RouteInitializer, implementedBy: AppRouter);
  }

}

class AppRouter implements RouteInitializer {

  init(Router router, ViewFactory view) {
   router.root
    ..addRoute(
      name: 'login',
      path: '/login',
      enter: view('app/views/login.tpl.html'))
    ..addRoute(
       defaultRoute: true,
       name: 'index',
       enter: view('app/views/index.tpl.html'));
  }

}

@NgController(selector: '[app-ctrl]', publishAs: 'ctrl')
class AppController {

}

@NgController(selector: '[login-ctrl]', publishAs: 'ctrl')
class LoginController {

  Http _http;
  String works = 'Works.';

  LoginController(this._http);

}
Run Code Online (Sandbox Code Playgroud)

没有路由正常工作,点击"#/ login"链接不会更改网址或视图.

记录说

clicked /app/web/index.html#/login
route /app/web/index.html [Route: null]
route  [Route: index]
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

小智 7

此代码可能存在一些问题.从我可以看到的最可能的问题是路由使用pushState.使用pushState时,不要使用散列操作url.有关详细信息,请参阅: 操作浏览器历史记录

Angular将在浏览器支持时使用推送状态.

bind(NgRoutingUsePushState, toValue: new NgRoutingUsePushState.value(false));
Run Code Online (Sandbox Code Playgroud)

给你的模块是:

class AppModule extends Module {

  AppModule(){
    bind(AppController);
    bind(LoginController);
    bind(RouteInitializer, implementedBy: AppRouter);
    bind(NgRoutingUsePushState, toValue: new NgRoutingUsePushState.value(false));
  }
}
Run Code Online (Sandbox Code Playgroud)

其他可能的问题包括:

  • 没有ng-view指令
  • 未在app/views/login.tpl.html和app/views/index.tpl.html中设置ng-bind-route

当我进行所有这些更改时,您的代码在导航到#/ login时正常工作