Angular中重定向后模态窗口消失。
创始人
2024-10-31 20:31:03
0

在Angular中,可以通过以下步骤解决重定向后模态窗口消失的问题:

  1. 创建一个服务来管理模态窗口状态。在该服务中,定义一个私有的Subject对象,用于订阅模态窗口的显示状态。
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';

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

  modalVisible$ = this.modalVisible.asObservable();

  show() {
    this.modalVisible.next(true);
  }

  hide() {
    this.modalVisible.next(false);
  }
}
  1. 在模态窗口组件中,使用该服务来管理模态窗口的显示状态。
import { Component, OnInit } from '@angular/core';
import { ModalService } from 'path/to/modal.service';

@Component({
  selector: 'app-modal',
  templateUrl: './modal.component.html',
  styleUrls: ['./modal.component.css']
})
export class ModalComponent implements OnInit {
  modalVisible = false;

  constructor(private modalService: ModalService) { }

  ngOnInit() {
    this.modalService.modalVisible$.subscribe(visible => {
      this.modalVisible = visible;
    });
  }

  close() {
    this.modalService.hide();
  }
}
  1. 在需要显示模态窗口的组件中,注入该服务,并在合适的时机调用show()方法来显示模态窗口。
import { Component } from '@angular/core';
import { ModalService } from 'path/to/modal.service';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.css']
})
export class HomeComponent {
  constructor(private modalService: ModalService) { }

  openModal() {
    this.modalService.show();
  }
}
  1. 当重定向发生时,保存模态窗口的显示状态。可以通过在路由守卫中监听路由变化,并在路由变化前保存模态窗口的显示状态。
import { Injectable } from '@angular/core';
import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree } from '@angular/router';
import { Observable } from 'rxjs';
import { ModalService } from 'path/to/modal.service';

@Injectable({
  providedIn: 'root'
})
export class SaveModalGuard implements CanActivate {
  constructor(private modalService: ModalService, private router: Router) { }

  canActivate(
    next: ActivatedRouteSnapshot,
    state: RouterStateSnapshot): Observable | Promise | boolean | UrlTree {

    // Save the modal visibility before redirecting
    sessionStorage.setItem('modalVisible', this.modalService.modalVisible.toString());

    return true;
  }
}
  1. 在重定向的目标组件中,恢复模态窗口的显示状态。可以通过在该组件的构造函数中读取并设置模态窗口的显示状态。
import { Component, OnInit } from '@angular/core';
import { ModalService } from 'path/to/modal.service';

@Component({
  selector: 'app-target',
  templateUrl: './target.component.html',
  styleUrls: ['./target.component.css']
})
export class TargetComponent implements OnInit {
  constructor(private modalService: ModalService) {
    const modalVisible = sessionStorage.getItem('modalVisible');
    if (modalVisible) {
      this.modalService.show();
    }
  }

  ngOnInit() {
    // Other initialization logic
  }
}

这样,当发生重定向时,模态窗口的显示状态将会被保存,并在重定向的目标组件中恢复。

相关内容

热门资讯

透视解迷"wepok... 透视解迷"wepoker怎么买辅助"确实真的有辅助器(哔哩哔哩)一、wepoker怎么买辅助游戏安装...
透视必备"wpk软件... 透视必备"wpk软件是正规的吗"果然存在有辅助脚本(哔哩哔哩)1、下载好wpk软件是正规的吗脚本下载...
透视透视"We po... 透视透视"We poker辅助器下载"竟然真的是有辅助脚本(哔哩哔哩)1.We poker辅助器下载...
透视必备"wepok... 透视必备"wepoker透视版下载"都是有辅助器(哔哩哔哩)运wepoker透视版下载辅助工具,进入...
透视分享"聚星ai辅... 透视分享"聚星ai辅助工具下载"好像是有辅助工具(哔哩哔哩)在进入聚星ai辅助工具下载软件靠谱后,参...
透视有挂"hhpok... 透视有挂"hhpoker辅助挂是真的吗"真是真的是有辅助神器(哔哩哔哩)1、该软件可以轻松地帮助玩家...
透视解谜"智星德州插... 透视解谜"智星德州插件最新版本更新内容详解"切实真的有辅助脚本(哔哩哔哩)1、透视解谜"智星德州插件...
透视辅助"wepok... 透视辅助"wepoker底牌透视"都是是真的辅助神器(哔哩哔哩)1、用户打开应用后不用登录就可以直接...
透视辅助"wepok... 透视辅助"wepoker透视脚本苹果版"一直是有辅助方法(哔哩哔哩)1)wepoker透视脚本苹果版...
透视透视"wepok... 您好,wepoker能不能透视这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302...