How do I get routes to work with AngularDart?

a 夏天 提交于 2019-12-10 13:33:47

问题


This is my code,

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

}

No routes are working, clicking on a '#/login' link does not change the url or the view.

Log says

clicked /app/web/index.html#/login
route /app/web/index.html [Route: null]
route  [Route: index]

What am I doing wrong?


回答1:


There might be a couple of problems with this code. From what I can see the most likely problem is that the routing is using pushState. When you use pushState you don't manipulate the url using a hash. For more information on that see: Manipulating Browser History

Angular will use push state when a browser supports it.

bind(NgRoutingUsePushState, toValue: new NgRoutingUsePushState.value(false));

Giving you are module of:

class AppModule extends Module {

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

Other possible problems include:

  • Not having an ng-view directive
  • Not setting the ng-bind-route in app/views/login.tpl.html, and app/views/index.tpl.html

When I made all these changes your code worked correctly for me when navigating to #/login



来源:https://stackoverflow.com/questions/20030527/how-do-i-get-routes-to-work-with-angulardart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!