App路由在窗口调整大小时未导航到正确的组件
创始人
2024-09-11 05:30:09
0次

在Angular应用中,我们可以使用RxJS的Subject和debounceTime操作符来处理窗口大小调整事件。在我们的服务/服务中,我们定义一个窗口大小变化的Subject,然后我们可以在需要的任何组件中订阅它。当窗口大小变化时,我们可以使用Angular路由器中的NavigationEnd事件来重新导航到正确的组件。

首先,我们定义一个服务来处理窗口大小变化:

import { Injectable } from '@angular/core'; import { Subject } from 'rxjs';

@Injectable({ providedIn: 'root' }) export class WindowResizeService { private _resizeSubject = new Subject();

public get resizeObserver$() { return this._resizeSubject.asObservable().pipe(debounceTime(300)); }

constructor() { window.addEventListener('resize', this.onResize.bind(this)); }

private onResize() { this._resizeSubject.next(); } }

然后,我们可以在需要的组件中订阅它,在回调函数中重新导航:

import { Component, OnDestroy } from '@angular/core'; import { Router } from '@angular/router'; import { Subscription } from 'rxjs'; import { WindowResizeService } from '../services/window-resize.service';

@Component({ selector: 'app-some-component', templateUrl: './some-component.component.html', styleUrls: ['./some-component.component.css'] }) export class SomeComponent implements OnDestroy { private _subscription: Subscription;

constructor(private _resizeService: WindowResizeService, private _router: Router) { this._subscription = this._resizeService.resizeObserver$.subscribe(() => { this.onWindowResize(); }); }

ngOnDestroy() { if (this._subscription) { this._subscription.unsubscribe(); } }

private onWindowResize() { // here we can navigate to the correct component this._router.navigate(['/some-route']); } }

这样,我们就可以通过订阅窗口大小变化事件来重新导航到正确的组件,以避免在调整窗口大小时出

相关内容

热门资讯

我来分享!wpk真的有透视嘛,... 『标题:当前标题』主打赛后复盘的 AI 游戏攻略黑科技走红,对局总结能力开挂。上传对局回放或者战绩截...
必赢方法!wpk辅助是什么,微... wpk辅助是什么AI 游戏攻略黑科技上线本地缓存功能,离线查阅能力开挂。提前下载对应游戏攻略数据包,...
实测必看!wepoker私人局... 『标题:当前标题』主打赛后复盘的 AI 游戏攻略黑科技走红,对局总结能力开挂。上传对局回放或者战绩截...
一分钟带你了解!wepoker... 一分钟带你了解!wepoker开脚本视频,德扑之星开挂透视教程神器(从来真的有透视挂)主打本地运行的...
推荐攻略!微扑克辅助下载,德普... 推荐攻略!微扑克辅助下载,德普之星确实有挂安装(最初存在有辅助器)AI 是一款可以让一直输的玩家修复...
热门推荐!we-poker辅助... 一款 AI 游戏攻略黑科技工具上线,查攻略效率直接开挂。它整合版本更新、装备搭配、BOSS 打法、牌...
一分钟带你了解!WPK脚本,w... 『标题:当前标题』主打赛后复盘的 AI 游戏攻略黑科技走红,对局总结能力开挂。上传对局回放或者战绩截...
揭秘攻略!wpk软件是正规的吗... 一款 AI 游戏攻略黑科技工具上线,查攻略效率直接开挂。它整合版本更新、装备搭配、BOSS 打法、牌...
今日重大通报!wepoker透... 今日重大通报!wepoker透视脚本安卓,微扑克透视软件工具(往昔真的是有开挂)AI 是一款可以让一...
一分钟了解!wpk模拟器是什么... wpk模拟器是什么AI 游戏攻略黑科技上线本地缓存功能,离线查阅能力开挂。提前下载对应游戏攻略数据包...