(ღ˘⌣˘ღ)嗯 2024-08-15 11:38 采纳率: 28.6%
浏览 1

angular iframe src发生改变页面不刷新

已经尝试的方法

  1. 添加参数
    http://localhost:4200/user/userDetails?userId=xx&time=xxxx
  2. 发生变化之后,先将iframeUrl = null,之后为iframeUrl重新复制
    问题都没有解决
@ViewChild("targetIframe", {static: true}) targetIframe?: HTMLIFrameElement;

  change(userId: string) {
    this.iframeSrc1 = null;
    const time = new Date().getTime();
    this.iframeSrc1 = `http://localhost:4200/user/userDetails?userId=${userId}&time=${time}`
    this.targetIframe?.contentWindow?.location.reload();
  }

contentWindow获取不到该怎么处理?

  • 写回答

2条回答 默认 最新

  • 一直免费一直爽 2024-08-15 11:38
    关注

    以下回复参考:皆我百晓生券券喵儿等免费微信小程序作答:

    你可以尝试使用ngOnChanges生命周期钩子来检测userId的变化,并在变化时更新iframe的src。这是一个示例:

    import { Component, OnChanges, SimpleChanges, ViewChild } from '@angular/core';
    
    @Component({
      selector: 'app-root',
      template: `
        <iframe #targetIframe [src]="iframeSrc1"></iframe>
      `,
    })
    export class AppComponent implements OnChanges {
      @ViewChild('targetIframe') targetIframe: HTMLIFrameElement;
      iframeSrc1 = '';
    
      ngOnChanges(changes: SimpleChanges) {
        if (changes.userId && changes.userId.currentValue !== changes.userId.previousValue) {
          this.updateIframeSrc();
        }
      }
    
      updateIframeSrc() {
        const time = new Date().getTime();
        this.iframeSrc1 = `http://localhost:4200/user/userDetails?userId=${this.userId}&time=${time}`;
        this.targetIframe.contentWindow.location.reload();
      }
    }
    

    在这个示例中,我们使用了ngOnChanges钩子来检测userId的变化。当userId发生变化时,我们调用updateIframeSrc方法来更新iframe的src并刷新页面。

    评论

报告相同问题?

问题事件

  • 修改了问题 8月15日
  • 创建了问题 8月15日

悬赏问题

  • ¥15 35114 SVAC视频验签的问题
  • ¥15 impedancepy
  • ¥15 在虚拟机环境下完成以下,要求截图!
  • ¥15 求往届大挑得奖作品(ppt…)
  • ¥15 如何在vue.config.js中读取到public文件夹下window.APP_CONFIG.API_BASE_URL的值
  • ¥50 浦育平台scratch图形化编程
  • ¥20 求这个的原理图 只要原理图
  • ¥15 vue2项目中,如何配置环境,可以在打完包之后修改请求的服务器地址
  • ¥20 微信的店铺小程序如何修改背景图
  • ¥15 UE5.1局部变量对蓝图不可见