Angular 7:如何从iframe访问URL中的查询字符串?

use*_*903 6 query-parameters angular angular7 angular-cli-v7

正在开发的应用程序托管在iframe中.

我在我的应用程序中配置了以下路由:

    const routes: Routes = [
  {
    path: '',
    pathMatch: 'full',
    redirectTo: 'list'
  },
  {
    path: 'list',
    component: ListComponent,
    canActivate: [CanActivateRoute]
  },
  {
    path: 'dashboard',
    loadChildren: './modules/dashboard/dashboard.module#DashboardModule'
  }
];

@NgModule({
  imports: [RouterModule.forRoot(routes, { enableTracing: true })],
  exports: [RouterModule],
  providers: []
})
export class AppRoutingModule { }
Run Code Online (Sandbox Code Playgroud)

ListComponent,OnInit方法,我用下面的代码来获取查询字符串PARAMS:

this.router.events.subscribe(val => {
      if (val instanceof RoutesRecognized) {
        console.log("val.state.root.firstChild.params: " + val.state.root.firstChild.params);
      }
    }); 
const firstParam: string = this.route.snapshot.queryParamMap.get('id');
Run Code Online (Sandbox Code Playgroud)

这些似乎都不起作用.我申请的网址如下:

https://company.com/#/app-dev/list?id=3

QueryParamMap或者RoutesRecongnized似乎没有在url中获取查询字符串"id = 3".它总是返回null.

任何人都可以帮我如何从角度应用程序中的URL检索查询参数/查询字符串?

小智 0

由于该应用程序位于 iframe 下。你应该使用 subscribe 方法来获取参数,而不是去获取快照。还要检查片段是否# 之后获取完整的 url