Angular状态URL中的查询参数是否传递给嵌套状态?
创始人
2024-11-01 10:30:15
0

在Angular中,查询参数默认情况下是不会传递给嵌套状态的。但是你可以通过使用paramsInheritanceStrategy选项来实现传递查询参数给嵌套状态。

下面是一个示例代码,展示了如何配置paramsInheritanceStrategy选项来传递查询参数给嵌套状态:

import { Component } from '@angular/core';
import { RouterModule, Routes, Router, ActivatedRoute, ParamMap } from '@angular/router';

@Component({
  selector: 'app-root',
  template: `
    

Parent Component

Query Param: {{queryParam}}

Go to Child `, }) export class AppComponent { queryParam: string; constructor(private route: ActivatedRoute) { this.queryParam = this.route.snapshot.queryParamMap.get('param'); } } @Component({ selector: 'child-component', template: `

Child Component

Query Param: {{queryParam}}

`, }) export class ChildComponent { queryParam: string; constructor(private route: ActivatedRoute) { this.queryParam = this.route.snapshot.queryParamMap.get('param'); } } const routes: Routes = [ { path: 'parent', component: AppComponent }, { path: 'parent/child', component: ChildComponent }, ]; @NgModule({ imports: [RouterModule.forRoot(routes, { paramsInheritanceStrategy: 'always' })], exports: [RouterModule] }) export class AppRoutingModule { }

在上面的代码中,我们在AppRoutingModule的配置中使用了paramsInheritanceStrategy: 'always'选项。这将使得查询参数在嵌套状态中被传递。

AppComponentChildComponent中,我们使用ActivatedRoute来获取当前状态的查询参数。通过使用this.route.snapshot.queryParamMap.get('param'),我们可以获取到名为param的查询参数的值。

AppComponent的模板中,我们使用[queryParams]绑定将查询参数传递给ChildComponent,并且使用[routerLink]来导航到ChildComponent

希望这个示例可以帮助你理解如何传递查询参数给嵌套状态。

相关内容

热门资讯

透视教你"wepok... 透视教你"wepoker有用吗"切实真的有辅助技巧(哔哩哔哩)wepoker有用吗破解侠是真的助透视...
透视必备"wpk刷入... 透视必备"wpk刷入池率脚本"真是真的是有辅助挂(哔哩哔哩)1)wpk刷入池率脚本辅助插件:进一步探...
透视透视"wepok... 透视透视"wepoker黑侠辅助器正版下载"都是真的有辅助教程(哔哩哔哩)wepoker黑侠辅助器正...
透视关于"wepok... 透视关于"wepoker私人局外卦"一贯是有辅助技巧(哔哩哔哩)1、wepoker私人局外卦辅助软件...
透视了解"wepok... 透视了解"wepoker透视破解版"好像真的是有辅助软件(哔哩哔哩)1、该软件可以轻松地帮助玩家将w...
透视分享"wepok... 透视分享"wepoker钻石怎么看底牌"都是存在有辅助器(哔哩哔哩)一、wepoker钻石怎么看底牌...
透视分享"wepok... 透视分享"wepoker可以透视码"都是有辅助软件(哔哩哔哩)wepoker可以透视码透视方法中分为...
透视解谜"steam... 透视解谜"steampokermaster辅助"原来有辅助方法(哔哩哔哩)进入游戏-大厅左侧-新手福...
透视普及"wepok... 透视普及"wepoker买脚本靠谱吗"一直有辅助神器(哔哩哔哩)1、透视普及"wepoker买脚本靠...
透视教你"aapok... 透视教你"aapoker万能辅助器"总是有辅助神器(哔哩哔哩)1、起透看视 aapoker万能辅助器...