基础窗口和弹出窗口之间的通信 Angular 12
当用户尝试再次打开同一个弹出窗口(这是一个齿轮图标下拉菜单)时,我需要触发从基本窗口到弹出窗口的一些事件。我可以通过哪些不同的方式来处理这个用例?我尝试使用服务,但由于基本窗口和弹出窗口是不同的角度应用程序,因此它创建了该服务的不同实例。
import { Injectable } from "@angular/core";
import { Subject } from "rxjs";
@Injectable()
export class ServiceMessage{
subscribers: any = [];
private subject = new Subject<any>();
sub = this.subject.asObservable();
a: any = [];
constructor() {}
nextSub(data: any) {
this.subject.next(data)
}
}
I need to trigger some event from the base window to a popup window when a user tries to open the same popup window again, which is a gear icon dropdown. What are the different ways I can approach this use case? I tried using a service but since the base window and popup window are different angular applications, it's creating a different instance of the service.
import { Injectable } from "@angular/core";
import { Subject } from "rxjs";
@Injectable()
export class ServiceMessage{
subscribers: any = [];
private subject = new Subject<any>();
sub = this.subject.asObservable();
a: any = [];
constructor() {}
nextSub(data: any) {
this.subject.next(data)
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决方案:
在本地存储中设置标志
从本地存储获取标志
window.postMessage()
方法安全地启用 Window 对象之间的跨域通信;例如,页面和它生成的弹出窗口之间,或者页面和嵌入其中的 iframe 之间。有关
window.postMessage()
的更多信息,请参见此处 https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessageSolutions:
Set a flag in local storage
Get the flag from local storage
Window.postMessage()
to communicate. Thewindow.postMessage()
method safely enables cross-origin communication between Window objects; e.g., between a page and a pop-up that it spawned, or between a page and an iframe embedded within it.More on
window.postMessage()
here https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage