angular - Angular2 Router 中有抽象状态吗?

标签 angular angular2-routing

我正在寻找与 ui-router abstract state 类似的解决方案在 Angular2 路由器中。

拥有“虚拟”父状态来解决子状态的一些常见问题。

最佳答案

路由不需要与组件相关联。它只能有一个空路径的 child 。您可以将此“空”路由用于守卫或解决之类的事情。

直到现在测试我才知道的一件事是 Resolve s(它的数据)将滴入子路由。因此,如果您想“解决子状态的一些常见问题”,这里可能是您的最佳选择。

{
  path: '',
  resolve: {
    data: DummyResolve
  },
  children: [
    { path: 'one', component: Child1Component },
    { path: 'two', component: Child2Component }
  ]
}

这是我用来测试的完整测试

import { Component, Injectable, OnInit } from '@angular/core';
import { Router, Resolve, ActivatedRoute } from '@angular/router';
import { By } from '@angular/platform-browser';
import { Location, CommonModule } from '@angular/common';
import { RouterTestingModule } from '@angular/router/testing';
import { TestBed, inject, fakeAsync, tick, ComponentFixture } from '@angular/core/testing';

@Injectable()
class DummyResolve implements Resolve<string> {
  resolve() {
    return 'Hello Routing';
  }
}

@Component({
  template: `
    <router-outlet></router-outlet>
  `
})
class RoutingComponent {
}

@Component({
  template: `
    <h1>Child One</h1>
    <span>{{ data }}</span>
  `
})
class Child1Component implements OnInit {
  data: string;

  constructor(private route: ActivatedRoute) {}

  ngOnInit() {
    this.route.data.forEach((data: { data: string }) => {
      this.data = data.data;
      console.log(`data from child 1: ${this.data}`);
    });
  }
}

@Component({
  template: `
    <h1>Child Two</h1>
    <span>{{ data }}</span>
  `
})
class Child2Component implements OnInit {
  data: string;

  constructor(private route: ActivatedRoute) {}

  ngOnInit() {
    this.route.data.forEach((data: { data: string }) => {
      this.data = data.data;
      console.log(`data from child 2: ${this.data}`);
    });
  }
}

describe('component: RoutingComponent', function () {
  let fixture: ComponentFixture<RoutingComponent>;

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [
        CommonModule,
        RouterTestingModule.withRoutes([
          {
            path: '',
            resolve: {
              data: DummyResolve
            },
            children: [
              { path: 'one', component: Child1Component },
              { path: 'two', component: Child2Component }
            ]
          }
        ])
      ],
      providers: [ DummyResolve ],
      declarations: [ RoutingComponent, Child1Component, Child2Component ]
    });

    fixture = TestBed.createComponent(RoutingComponent);
    fixture.detectChanges();
  });

  it('should go to child one',
    fakeAsync(inject([Router, Location], (router: Router, location: Location) => {

    router.navigate(['/one']);
    tick();
    fixture.detectChanges();
    let debugEl = fixture.debugElement;
    expect(debugEl.query(By.css('h1')).nativeElement.innerHTML).toEqual('Child One');
    expect(debugEl.query(By.css('span')).nativeElement.innerHTML).toEqual('Hello Routing');
  })));

  it('should go to child two',
    fakeAsync(inject([Router, Location], (router: Router, location: Location) => {

    router.navigate(['/two']);
    tick();
    fixture.detectChanges();
    let debugEl = fixture.debugElement;
    expect(debugEl.query(By.css('h1')).nativeElement.innerHTML).toEqual('Child Two');
    expect(debugEl.query(By.css('span')).nativeElement.innerHTML).toEqual('Hello Routing');
  })));
});

关于angular - Angular2 Router 中有抽象状态吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39579830/

相关文章:

angular - 我的多路由器 socket 链接在 Angular 中不起作用

javascript - 如何在 Angular 中获取 DOM 节点属性

angular - 禁用滑动以查看sidemenu ionic 2

angular - 知道父级中选择了哪个子 RouteSegment 吗?

Angular 2,永远不会为实现 OnActivate 的组件调用 routerOnActivate 方法

angular - 如何在 Angular 2 中创建单例服务?

angular - 在 router-outlet 注入(inject)的延迟加载模块中使用面包屑组件

angularjs - 在 Angular 2 中使用 Angular 1 库?

angular - 如何在 Angular 4.x 应用程序中使用 Bower 管理前端依赖项

AngularJS 2 Dart 获取 RouterLink 参数