Angular能够正确地进行路由,但是子路由的地址栏不会改变。
创始人
2024-10-28 16:01:12
0

要正确进行路由,同时在子路由中改变地址栏,可以使用Angular的Router模块提供的NavigationExtras参数。

在父组件的路由配置中,使用children属性来定义子路由。然后,在子组件中使用Router服务的navigate方法来导航到子路由,并传递NavigationExtras参数。

以下是一个示例:

在父组件的路由配置中:

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { ParentComponent } from './parent.component';
import { ChildComponent } from './child.component';

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

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

在父组件的模板中,添加一个用于导航到子路由的链接:

Go to Child

在子组件中,使用Router服务的navigate方法来导航到子路由,并传递NavigationExtras参数来指定要改变地址栏的行为:

import { Component } from '@angular/core';
import { Router, NavigationExtras } from '@angular/router';

@Component({
  selector: 'app-child',
  template: '

Child Component

' }) export class ChildComponent { constructor(private router: Router) { } navigateToParent() { const extras: NavigationExtras = { queryParams: { 'param': 'value' }, // 设置查询参数 fragment: 'section1', // 设置片段 skipLocationChange: false, // 默认为false,设置为true时地址栏不改变 replaceUrl: false // 默认为false,设置为true时替换浏览历史记录 }; this.router.navigate(['/parent'], extras); } }

在子组件的模板中,添加一个按钮,点击时调用navigateToParent方法:


通过这样配置,当你点击"Go to Child"链接时,地址栏会显示"/parent/child"。然后,当你在子组件中点击"Go to Parent"按钮时,地址栏会改变为"/parent",同时保留查询参数和片段。

希望这能帮到你!

相关内容

热门资讯

透视能赢!wejoker辅助脚... 透视能赢!wejoker辅助脚本,we poker辅助器v3.3(透视)AA德州教程(有挂工具)1、...
透视教学!wepoker有辅助... 透视教学!wepoker有辅助功能吗,wepoker辅助是真的吗(透视)wpk教程(有挂解密);1、...
透视科技!wejoker透视方... 透视科技!wejoker透视方法,wepoker软件靠谱么(透视)细节方法(有挂脚本);1、实时we...
透视工具!竞技联盟破解版最新版... 透视工具!竞技联盟破解版最新版,wepoker游戏安装教程,揭秘教程(有挂规律)1、在竞技联盟破解版...
透视安卓版!wepoker游戏... 透视安卓版!wepoker游戏安装教程,wepoker轻量版透视方法(透视)揭秘教程(有挂教程)1、...
透视脚本!hhpoker透视脚... 透视脚本!hhpoker透视脚本视频,德普之星透视,细节方法(有挂技巧);1、金币登录送、破产送、升...
透视辅助!德普之星辅助工具如何... 透视辅助!德普之星辅助工具如何设置,aapoker怎么设置提高好牌几率,安装教程(有挂详情);在进入...
透视能赢!wepoker作弊辅... 透视能赢!wepoker作弊辅助,wepoker作弊方法(透视)透明挂教程(有挂解密)1)wepok...
透视辅助!wepoker有没有... 透视辅助!wepoker有没有透视方法,wejoker内置辅助,2025新版(有挂秘籍)1、操作简单...
透视软件!wepoker钻石怎... 透视软件!wepoker钻石怎么看底牌,wepoker有人用过吗(透视)黑科技教程(有挂细节)wep...