Angular组件不拾取手动触发的Windows调整大小事件

发布于 2025-01-17 09:35:17 字数 596 浏览 1 评论 0原文

有一个在调整窗口大小时重新渲染的组件(并且效果很好)。但是,页面上还有一些其他组件会导致元素出现/消失。每当可用空间发生变化时,我的组件就需要重新渲染。

由于此组件仅在应用程序中出现一次,因此我认为监听这样的窗口大小调整事件是安全的...

  @HostListener('window:resize') // re-render if window is resized
  private render() {
    // code that causes the contents of the component to re-render
  }

这部分有效。对于折叠和展开页面区域的其他同级组件,我想我可以像这样调用 windows.dispatchEvent ...

  toggleRightSidebar() {
    window.dispatchEvent(new Event('resize'));
    this.settingsButtonClicked.emit();
  }

但是具有 @HostListener 的同级组件不会检测到以这种方式触发的窗口调整大小事件。

我错过了什么吗?

Have a component that re-renders when the window is resized (and this works well). However, there are some other components on the page that cause elements to appear/disappear. My component needs to re-render whenever the available space changes.

As this component only appears once in the app, I thought it would be safe to listen to window resize events like this...

  @HostListener('window:resize') // re-render if window is resized
  private render() {
    // code that causes the contents of the component to re-render
  }

This part works. For the other sibling components that collapse and expand areas of the page, I thought I could just call windows.dispatchEvent like this...

  toggleRightSidebar() {
    window.dispatchEvent(new Event('resize'));
    this.settingsButtonClicked.emit();
  }

But the sibling component that has the @HostListener doesn't detect a windows resize event triggered in this manner.

Am I missing something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文