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。

相关内容

热门资讯

从来有辅助"温州茶园... 较多好评“微乐万能挂官网”开挂(透视)辅助教程 了解更多开挂安装加(136704302)微信号是一款...
一向有辅助"wepo... wepoker辅助器免费下载是一款专注玩家量身打造的游戏记牌类型软件,在wepoker辅助器免费下载...
本来有辅助"poke... 本来有辅助"pokemmo手机辅助软件"开挂(透视)辅助软件(玩家教你有挂功能);无需打开直接搜索加...
往昔有开挂"微信卡五... 往昔有开挂"微信卡五星辅助"开挂(透视)辅助透视(透视教程有挂规律) 【无需打开直接搜索加薇1367...
固有有辅助"福建兄弟... 固有有辅助"福建兄弟十三水有没有挂"开挂(透视)辅助下载(分享教程有挂教学);无需打开直接搜索加薇1...
本然有开挂"打两圈兴... 本然有开挂"打两圈兴化辅助"开挂(透视)辅助安装(揭秘教程有挂分享)《详细加薇136704302咨询...
从前有辅助"wepo... wepoker插件程序激活码是一款专注玩家量身打造的游戏记牌类型软件,在wepoker插件程序激活码...
往昔有辅助"小程序可... 往昔有辅助"小程序可以开挂吗"开挂(透视)辅助插件(高科技教程有挂教学)您好:小程序可以开挂吗这款游...
先前有开挂"钱塘十三... 钱塘十三水透视 无需打开直接搜索微信:136704302本司针对手游进行,选择我们的四大理由: 1、...
原先有开挂"决战卡五... 原先有开挂"决战卡五星游戏修改器"开挂(透视)辅助工具(软件教程有挂攻略)这是一款可以让一直输的玩家...