angular - 在 Angular2 Dart 中设置 Router & RouterLink 的正确方法是什么

标签 angular angular-ui-router dart

问题:设置 Router 的正确方法是什么? & RouterLinkAngular2 Dart .

主.dart

import 'package:angular2/angular2.dart';
import 'package:angular2/router.dart';

import 'package:angular2/src/reflection/reflection.dart' show reflector;
import 'package:angular2/src/reflection/reflection_capabilities.dart' show ReflectionCapabilities;


@Component(
    selector: 'home'
)
@View(
    template: '<h1>I am Home</h1><a router-link="child">Go Child</a>',
    directives: const [RouterOutlet, RouterLink]
)
class Home {}

//
//
//

@Component(
  selector: 'child'
)
@View(
    template: '<h1>I am Child</h1><a router-link="home">Go Home</a>',
    directives: const [RouterOutlet, RouterLink]
)
class Child {}

//
//
//

@Component(
  selector: 'index'
)
@View(
  template: '''
  <router-outlet></router-outlet>
            ''',
  directives: const [RouterOutlet, RouterLink]
)
class Index {
  Router router;

  Index(Router this.router) {
    router.config({ 'path': '/child', 'component': Child, 'alias': 'child'});
    router.config({ 'path': '/', 'component': Home, 'alias': 'home'});
  }

}

main() {
  reflector.reflectionCapabilities = new ReflectionCapabilities();
  bootstrap(Index, routerInjectables);
}

这是我的方法:

router_link.dart我看到 newHref回来作为 null

onAllChangesDone() {
    if (isPresent(this._route) && isPresent(this._params)) {
      var newHref = this._router.generate(this._route, this._params);
      this._href = newHref;
      // Keeping the link on the element to support contextual menu `copy link`

      // and other in-browser affordances.
      print('newHref');
      print(newHref);
      DOM.setAttribute(this._domEl, "href", newHref);
    }

这会导致错误并终止导航请求。

String expected STACKTRACE: 0 BlinkElement.setAttribute_Callback_2 (dart:_blink:7565)

1 BlinkElement.setAttribute_Callback_2_ (dart:_blink:7566)

2 Element.setAttribute (dart:html:13673)

3 BrowserDomAdapter.setAttribute(package:angular2/src/dom/browser_adapter.dart:258:25)

4 RouterLink.onAllChangesDone(package:angular2/src/router/router_link.dart:66:23)

最佳答案

从 Angular Dart 2.0.0(候选发布版)开始,correct syntax对于路由器链接是:

<a [router-link]="['./Home']">Go Home</a>

该值是传递给 Router.navigate() 的参数列表.

The correct syntax用于配置路由的是:

@Component(
    selector: 'my-app',
    template: ...,
    directives: const [ROUTER_DIRECTIVES],
    providers: const [HeroService, ROUTER_PROVIDERS])
@RouteConfig(const [
  const Route(
      path: '/dashboard',
      name: 'Dashboard',
      component: DashboardComponent,
      useAsDefault: true),
  const Route(
      path: '/detail/:id', name: 'HeroDetail', component: HeroDetailComponent),
  const Route(path: '/heroes', name: 'Heroes', component: HeroesComponent)
])
class AppComponent {
  String title = 'Tour of Heroes';
}

关于angular - 在 Angular2 Dart 中设置 Router & RouterLink 的正确方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30292484/

相关文章:

dart - 抽屉导航不适用于屏幕内的屏幕

list - Dart语言: Creating and accessing dynamic Lists

javascript - 页面刷新后,ui-router 看不到 url 搜索参数

javascript - UI 路由器从子节点访问父状态

angular - 公司代理背后的 Protractor (Angular-CLI)

javascript - 类型 'checked' .ts(2339) Angular 上不存在属性 'EventTarget'

Angular 2 - 将 URL 匹配到路由

flutter - 当 Hive 数据在 FireBaseMessage 的 onBackgroundMessage 处理程序中更新时,如何更新 UI 小部件?

java - 如何在 Angular 中调用 Java HTTP POST 方法

angular - 使用 <mat-tab> 时 Angular 6 上的 ExpressionChangedAfterItHasBeenCheckedError