【问题标题】:Routing transition between 2 components without destroying first one?2个组件之间的路由转换而不破坏第一个?
【发布时间】:2016-08-16 18:16:01
【问题描述】:

搜索了一整天后,我被困住了,所以我想寻求帮助。

如何在 angular2 中实现这样的功能: http://dfsq.github.io/ngView-animation-effects/app/#/page/1

我的意思是如何使用 Router 在 2 个页面之间无缝转换? 我可以为页面更改设置动画,但现在是这样的:

animation-in->第一页加载->animation-out->空路由器出口(组件被销毁)->animation-in->第二页加载

但我想要:

动画输入->第一页加载->第一页动画输出->第二页动画输入->第二页加载->这里销毁第一页

任何帮助将不胜感激:)

【问题讨论】:

  • 我认为目前没有简单的解决方案,但 Angular 团队正在致力于动画支持 AFAIK。
  • 嗯,是的,我是这么认为的,但是任何指向硬解决方案的指针? :) Angular 中的什么负责删除/添加组件到路由器插座?例如,我可以扩展路由器插座以延迟删除“prev”路由组件吗?
  • 嗯,不是我看源码。您所需要的一切都发生在RouterOutlet 中。通常使用自定义实现进行授权。我认为为您的目的创建自定义实现也不应该太难。示例见captaincodeman.com/2016/03/31/angular2-route-security

标签: angular angular2-routing


【解决方案1】:

好的,所以我开始工作了,如果有人感兴趣,这是我的解决方案 - 欢迎任何指向更好的解决方案,因为我是 Angular 世界中的菜鸟 :)

public activate(nextInstruction: ComponentInstruction): Promise<any> {
      let previousInstruction = this.currentInstruction;

      this.currentInstruction = nextInstruction;

      let componentType = nextInstruction.componentType;
      let childRouter = this.parentRouter.childRouter(componentType);

      let providers = ReflectiveInjector.resolve([
          provide(RouteData, {useValue: nextInstruction.routeData}),
          provide(RouteParams, {useValue: new RouteParams(nextInstruction.params)}),
          provide(routerMod.Router, {useValue: childRouter})
      ]);

      this.previousComponentRef = this.currentComponentRef;

      return this.loader.loadNextToLocation(componentType, this.currentElementRef, providers)
          .then((componentRef) => {

              this.currentComponentRef = componentRef;

              Observable.of(true).delay(100).toPromise().then(response => {
                if(this.previousComponentRef){
                  this.previousComponentRef.location.nativeElement.className = 'animateout';
                }
                this.currentComponentRef.location.nativeElement.className = 'animatein';
              });

              if (hasLifecycleHook(hookMod.routerOnActivate, componentType)) {
                  return (<OnActivate>componentRef.instance)
                      .routerOnActivate(nextInstruction, previousInstruction);
              }
          });


}


public routerCanDeactivate(nextInstruction: ComponentInstruction): Promise<boolean> {
    if (isBlank(this.currentInstruction)) {
        return this.resolveToTrue;
    }

    let ref = this.currentComponentRef;

    Observable.of(true).delay(2000).toPromise().then(response => {
        ref.destory();
    });

    return this.resolveToTrue;
}

如你所见,ai 扩展了 router-outlet 并实现了上述两个方法,第一个是向组件添加动画类,第二个是等待组件 dispose 允许动画,这里是示例动画 (dm-page,dm-home,dm -contact 是组件选择器):

dm-page, dm-home, dm-contact{position: fixed;top:100%; left:0; width: 100%; height: 100%;
 -webkit-transition: top .8s ease-in-out;
-moz-transition: top .8s ease-in-out;
-o-transition: top .8s ease-in-out;
transition: top .8s ease-in-out;}

.animatein {top:0;}
.animateout {top:-100%;}

【讨论】:

  • 更新 .dispose()destroy() 从 beta.16 开始
  • 嗨,我已将 .dispose 更新为 .destroy,并将 Injector 更新为 ReflectiveInjector,因为我今天更新为 beta.17。我还有一个问题——当我嵌套了动画出口时,即使只导航子路径,它也会为父组件和子组件设置动画。导航子路由时有什么方法可以不初始化根动画出口?
  • 能否将此代码更新为 RC4?喜欢用新的路由api和新的动态加载api吗?
  • 我认为最好等待 rc5 甚至更晚,从我一直在阅读的 angular2 动画来看,它会比我的简单解决方案做得更好
  • Angular 5(Beta)更新:弃用了 ReflectiveInjector 并引入了 StaticInjector。
猜你喜欢
  • 1970-01-01
  • 2016-07-27
  • 2016-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-28
  • 1970-01-01
  • 2016-10-18
相关资源
最近更新 更多