angular7中路由传值

本文详细介绍了在Angular中如何在不同路由间双向传递参数的方法,包括从起始路由到目标路由,以及从目标路由返回起始路由的完整过程。通过实例展示了如何使用Router和ActivatedRoute服务来实现参数的传递与接收。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在路由传值中,除了从开始路由到目标路由传值之外,还可以从目标路由传值到开始路由。

意思就是从开始路由传值到目标路由成功之后,执行完操作之后又从目标路由返回到了开始路由并携带参数。

一、起始路由向目标路由传递参数
  1. 获取路由对象
import { Router, ActivatedRoute, NavigationExtras} from '@angular/router';
  1. 注入
constructor(
    public router: Router,
    public activatedRoute: ActivatedRoute
) {}
  1. 获取当前激活的路由
 this.nowRouter = this.activatedRoute['_routerState'].snapshot.url;
  1. 跳转并传参
// 详情
FaultDetails(item: any) {
    const routerParams: NavigationExtras  = {
        queryParams: {
            nowRouter: this.nowRouter,   // 字符串
            nowRowTableData: JSON.stringify(item),    // 对象
            searchForm: JSON.stringify(this.searchForm)   // 对象
        }
    };
    this.router.navigate(['/faultDetails'], routerParams);
}
二、目标路由接收参数
  1. 获取路由对象
import { Router, ActivatedRoute, NavigationExtras} from '@angular/router';
  1. 获取参数
ngOnInit() {

        this.activatedRoute.queryParams.subscribe((data: any) => {

            const nowRowTableData = JSON.parse(data.nowRowTableData);

            const searchForm = JSON.parse(data.searchForm);

            this.searchForm = searchForm;

            // 父级路由
            if (data.nowRouter) {
                // 截取路由,否则报错,如果用到传递过来的路由并返回的时候携带参数,路由可能会不匹配,是因为路由后面多了一些参数,需要截取(第一次ok,第二次失败)
                const index = data.nowRouter.indexOf('?');
                if (index > 0) {
                    const routerUrl = data.nowRouter.substring(0, index);
                    this.parentRouter = routerUrl;
                } else {
                    this.parentRouter = data.nowRouter;
                }
            }

            // 当前的故障列表数据
            this.faultListData = nowRowTableData;

            // 调用故障详情
            this.getFaultDetails(nowRowTableData.RelationGUID);

            // 申请状态
            this.applyProState = nowRowTableData.ApplyProState;

            // 是否撤销
            this.isCancel = nowRowTableData.IsCancel;
        });
    }
三、目标路由向起始路由传递参数
  1. 获取路由对象
import { Router, ActivatedRoute, NavigationExtras} from '@angular/router';
  1. 注入同上
  2. 传参
 // 返回到父级
 backParent() {
     const routerParams: NavigationExtras  = {
         queryParams: {
             searchForm: JSON.stringify(this.searchForm),
         }
     };
     this.router.navigate([this.parentRouter], routerParams);
 }
四、起始路由接收参数
// 获取当前激活的路由传递过来的数据
this.activatedRoute.queryParams.subscribe((data: any) => {
    // 传递过来的搜索条件
    if (JSON.stringify(data) !== '{}') {
        const searchForm = JSON.parse(data.searchForm);
        // this.searchForm.startTime = searchForm.startTime;
        // this.searchForm.endTime = searchForm.endTime;
        // this.searchForm.ferSiteID = searchForm.ferSiteID;
        this.searchForm = searchForm;
        this.searchOrderData(null);
    }
});
### Angular路由嵌套的使用方法 在 Angular 应用程序中实现路由嵌套是一项重要的功能,它允许创建复杂的应用布局并管理多个组件之间的关系。通过 `ui-router` 或者 Angular 自带的 `Router` 模块都可以轻松完成这一操作。 对于 Angular 的原生路由模块来说,在定义子路径时可以通过指定父级组件中的 `<router-outlet>` 来加载对应的子组件[^1]。下面是一个简单的例子来展示如何设置父子级别的路由: #### 定义模块和导入必要的包 首先确保已经在应用的主要模块文件(通常是 app.module.ts)里引入了 RouterModule 并设置了预设好的根级别路由配置。 ```typescript import { NgModule } from '@angular/core'; import { Routes, RouterModule } from '@angular/router'; const routes: Routes = [ { path: '', redirectTo: '/home', pathMatch: 'full' }, { path: 'home', component: HomeComponent, children: [ { path: 'child-one', // 子路由一 component: ChildOneComponent }, { path: 'child-two', // 子路由二 component: ChildTwoComponent } ] } ]; @NgModule({ imports: [RouterModule.forRoot(routes)], exports: [RouterModule] }) export class AppRoutingModule {} ``` 这段代码展示了如何在一个名为 home 的顶级路由下添加两个不同的子路由 child-one 和 child-two 。当访问 `/home/child-one` 或者 `/home/child-two` URL 地址的时候就会分别显示相应的子页面。 #### 创建带有 Outlet 的 Parent 组件模板 为了让这些子路由能够正常工作,还需要在 parent 组件 (HomeComponent) 的 HTML 文件内放置一个特殊的标记 `<router-outlet></router-outlet>`, 这样就可以让 Angular 知道在哪里渲染匹配到的子组件了。 ```html <!-- home.component.html --> <div> <h2>Welcome to Home Page</h2> <!-- Navigation links for nested routes --> <nav> <a routerLink="child-one">Go To Child One</a><br/> <a routerLink="child-two">Go To Child Two</a> </nav> <!-- Placeholder where the matched child route will be rendered --> <router-outlet></router-outlet> </div> ``` 以上就是关于 Angular 中基本的路由嵌套方式介绍以及具体实施步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值