如何获取使用 AngularDart 的路线?

2024-04-17

这是我的代码,

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

}

没有路由有效,单击“#/login”链接不会更改 URL 或视图。

Log says

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

我究竟做错了什么?


这段代码可能存在一些问题。据我所知,最有可能的问题是路由正在使用pushState。当您使用 PushState 时,您不会使用哈希来操作 url。有关详细信息,请参阅:操纵浏览器历史记录 https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Manipulating_the_browser_history

当浏览器支持时,Angular 将使用推送状态。

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

为您提供以下模块:

class AppModule extends Module {

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

其他可能的问题包括:

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

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何获取使用 AngularDart 的路线? 的相关文章

随机推荐