【发布时间】:2019-01-10 19:34:23
【问题描述】:
我有一个全局加载器,它是这样实现的:
核心模块:
router.events.pipe(
filter(x => x instanceof NavigationStart)
).subscribe(() => loaderService.show());
router.events.pipe(
filter(x => x instanceof NavigationEnd || x instanceof NavigationCancel || x instanceof NavigationError)
).subscribe(() => loaderService.hide());
加载器服务:
@Injectable({
providedIn: 'root'
})
export class LoaderService {
overlayRef: OverlayRef;
componentFactory: ComponentFactory<LoaderComponent>;
componentPortal: ComponentPortal<LoaderComponent>;
componentRef: ComponentRef<LoaderComponent>;
constructor(
private overlay: Overlay,
private componentFactoryResolver: ComponentFactoryResolver
) {
this.overlayRef = this.overlay.create(
{
hasBackdrop: true,
positionStrategy: this.overlay.position().global().centerHorizontally().centerVertically()
}
);
this.componentFactory = this.componentFactoryResolver.resolveComponentFactory(LoaderComponent);
this.componentPortal = new ComponentPortal(this.componentFactory.componentType);
}
show(message?: string) {
this.componentRef = this.overlayRef.attach<LoaderComponent>(this.componentPortal);
this.componentRef.instance.message = message;
}
hide() {
this.overlayRef.detach();
}
}
使用 Angular 7.0.2 运行时,行为(我想要的)是:
- 在解析附加到路由的数据以及加载惰性模块时显示加载器
- 导航到没有任何解析器的路线时不显示加载程序
我已经更新到 Angular 7.2,现在的行为是:
- 在解析附加到路由的数据以及加载惰性模块时显示加载器
- 在导航到没有任何解析器的路线时显示没有
LoaderComponent的叠加层
我在NavigationStart和NavigationEnd事件上添加了一些日志,我发现NavigationEnd在NavigationStart之后立即触发(这是正常的),而Overlay在大约0.5秒后消失。
我已经阅读了CHANGELOG.md,但我没有发现任何可以解释这个问题的东西。欢迎任何想法。
编辑:
经过进一步研究,我通过这样设置package.json恢复了之前的行为:
"@angular/cdk": "~7.0.0",
"@angular/material": "~7.0.0",
而不是这个:
"@angular/cdk": "~7.2.0",
"@angular/material": "~7.2.0",
我已经确定了在 7.1.0 版中发布的错误提交,并将我的问题发布在相关的 GitHub issue 上。它修复了Overlay 的淡出动画。
获得所需行为的符合 v7.1+ 标准的方法是什么?
据我说,最好的办法是:仅在必要时显示加载程序,但NavigationStart 不包含所需的信息。
我想避免最终出现一些反跳行为。
【问题讨论】:
-
loaderService.hide()是否有可能在没有触发器的情况下执行? -
你问的是不是从别处调用的?
-
这可能是我从未考虑过的一个选项,但我的意思是,它可以在没有任何触发器的情况下执行,并且您使用的符号仅被解释为要执行的代码,而不是带有函数的 OOP 结构。
-
@David 对不起,我真的不明白你的意思
-
我在 Angular CDK 中发现了错误的拉取请求:github.com/angular/material2/pull/10145
标签: javascript angular rxjs angular-cdk