Angular路由器正在从iframe中捕获URL。
创始人
2024-10-28 03:33:15
0

要在Angular路由器中捕获iframe中的URL,可以使用Angular的ActivatedRoute服务。以下是一个示例代码:

  1. 在父组件的HTML模板中,添加一个iframe元素,并指定src属性为要捕获的URL:

  1. 在父组件的Typescript代码中,定义一个iframeUrl变量,并在构造函数中初始化它:
import { Component } from '@angular/core';

@Component({
  selector: 'app-parent',
  templateUrl: './parent.component.html',
  styleUrls: ['./parent.component.css']
})
export class ParentComponent {
  iframeUrl: string;

  constructor() {
    this.iframeUrl = 'https://example.com'; // 替换为实际的iframe URL
  }
}
  1. 在子组件的构造函数中注入ActivatedRoute服务,并使用它来获取URL参数:
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';

@Component({
  selector: 'app-child',
  templateUrl: './child.component.html',
  styleUrls: ['./child.component.css']
})
export class ChildComponent implements OnInit {
  constructor(private route: ActivatedRoute) { }

  ngOnInit(): void {
    this.route.queryParams.subscribe(params => {
      const url = params['url'];
      console.log('URL from iframe:', url);
      // 在这里处理从iframe中捕获到的URL
    });
  }
}
  1. 在父组件的路由配置中,将子组件添加为一个路由:
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { ParentComponent } from './parent.component';
import { ChildComponent } from './child.component';

const routes: Routes = [
  { path: '', component: ParentComponent, children: [
    { path: 'child', component: ChildComponent }
  ] }
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})
export class ParentRoutingModule { }

现在,当iframe加载完成并传递URL参数时,子组件的ngOnInit方法将被调用,并且可以通过路由参数访问到URL。

相关内容

热门资讯

第6分钟了解!人人燕赵辅助下载... 第6分钟了解!人人燕赵辅助下载!一直一直都是有辅助app(确实有挂)-哔哩哔哩1、超多福利:超高返利...
4分钟了解!皮皮衡阳辅助!一贯... 4分钟了解!皮皮衡阳辅助!一贯是真的有辅助方法(确实有挂)-哔哩哔哩亲,关键说明,皮皮衡阳辅助透视脚...
第九分钟了解!青橙竞技辅助器怎... 第九分钟了解!青橙竞技辅助器怎么安装!切实一直都是有辅助软件(有挂方法)-哔哩哔哩1、游戏颠覆性的策...
两分钟了解!小闲川南辅助工具!... 两分钟了解!小闲川南辅助工具!本来真的有辅助方法(有挂头条)-哔哩哔哩1、小闲川南辅助工具破解器简单...
第七分钟了解!传送屋激k怎么开... 第七分钟了解!传送屋激k怎么开挂!一贯存在有辅助教程(有挂细节)-哔哩哔哩1、许多玩家不知道传送屋激...
六分钟了解!pokemmo手机... 六分钟了解!pokemmo手机版脚本免费!原来一直都是有辅助攻略(有挂详情)-哔哩哔哩pokemmo...
第9分钟了解!潮友会鱼虾蟹脚本... 第9分钟了解!潮友会鱼虾蟹脚本辅助器!果然有辅助神器(有挂方针)-哔哩哔哩亲,关键说明,潮友会鱼虾蟹...
第二分钟了解!新道游智能辅助器... 第二分钟了解!新道游智能辅助器!都是真的是有辅助插件(有挂技巧)-哔哩哔哩新道游智能辅助器是不是有人...
4分钟了解!小程序辅助器免费!... 您好,小程序辅助器免费这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】很多玩...
第八分钟了解!全民内蒙古辅助!... 第八分钟了解!全民内蒙古辅助!都是真的是有辅助插件(有挂教程)-哔哩哔哩1、全民内蒙古辅助脚本辅助下...