【问题标题】:Unable to read Route Parameter in Angular 2无法读取 Angular 2 中的路由参数
【发布时间】:2017-04-12 06:54:16
【问题描述】:

我已经在 Angular JS 中为我​​在同一个项目中的其他组件成功实现了路由参数,对于新组件,我也遵循相同的方式,但它不起作用,我无法理解代码中的问题。

下面是我的路由文件的代码

import { Routes, RouterModule } from '@angular/router';
import { CustomerBillComponent } from '../components/customer_bill.component';

const routes: Routes = [
    { path: 'add-bill', component: CustomerBillComponent },
    { path: 'add-bill/:customer_reference', component: CustomerBillComponent },
];

export const routing = RouterModule.forRoot(routes);

(我也尝试使用不同的路线,但没有成功)

下面是我的 CustomerBillComponent.ts 文件中的代码

ngOnInit() {
    //When I visit /#/add-bill/CR452152
    let route_location = location['hash'].split('/')[1].toLowerCase();
    console.log(route_location); //prints add-bill in Console
    var customer_reference = this.route.snapshot.params['customer_reference'];
    console.log(customer_reference); //prints undefined
}

我错过了什么

提前致谢!!!

【问题讨论】:

  • 同样的问题,同样的结果
  • 你找到解决办法了吗?
  • 您可能需要使用paramMap 而不是params。即:this.route.snapshot.paramMap.get('customer_reference') 这对我有用

标签: javascript angular angular2-routing angular-routing


【解决方案1】:

我面临几乎相同的问题。但原因不同。

在我的场景中,借出页面(路由指定的组件)不同,我试图访问父路由组件上的路由参数。

下面是演示我的场景的代码:

home.module.routing.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
//other import statements...
const routes: Routes = [
  {
    path: 'home', component: HomeComponent,
    children: [
      { path: 'buy/:mainType/:subType', component: BuyComponent, data: {} },
      { path: 'buy/:mainType', component: BuyComponent, data: { } },
      { path: '', redirectTo: 'buy', pathMatch: 'full' },
    ]
  },
  { path: 'checkout', component: CheckoutCartComponent },
];

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

所以,我必须访问 HomeComponent(Parent) 中的 RouteParam 值,而 Lending 页面是 BuyComponent(HomeComponent 的子路由)。

所以,我没有使用以下代码获取路由参数的值:

//HomeComponent.ts内部

ngOnInit() {
    this.sub = this.route.params.subscribe(params => {
        this.routeText = params['category'];
        this.title = this.formatTitle(this.routeText);
    });
}

由于借贷页面是BuyComponent,所以上面的代码是行不通的。 Angular 只允许在登陆组件上以上述方式访问路由参数。

我尝试了很多,终于找到了访问父路由组件中的路由参数的解决方案。

下面是访问相同的代码:

//HomeComponent.ts内部

const routeChildren = this.activatedRoute.snapshot.children;
if (routeChildren.length) {
    //Your code here to assign route params to the Component's Data Member.
}

所以,访问 this.activatedRoute.snapshot.children 而不是 this.route.params 对我来说是成功的。

如果有人像我一样来到这里寻找问题,希望这会有所帮助。

最好的问候。

【讨论】:

    【解决方案2】:

    您需要添加 ModuleWithProviders。它是模块提供者的包装器。

    import {ModuleWithProviders} from '@angular/core';
    
    export const routing: ModuleWithProviders = RouterModule.forRoot(router);
    

    【讨论】:

    • 请提供您的路线文件。泰
    • 我试过了,但customer_reference还是undefined,谢谢帮助...
    • 你确定在构造函数中声明了路由吗?
    • 我认为你应该使用 paramMap 而不是 params。 this.route.snapshot.paramMap.get('customer_reference');
    • 在我的构造函数 private route: ActivatedRoute 中,当我将 Param 更改为 paramMap 时,它说 ActiveRouteSnapshot 上不存在属性 paramMap
    【解决方案3】:

    第一次尝试:

    this.route.params.subscribe(
      (params) => {
        console.log(params);
      }
    )
    

    然后看看你会得到什么。如果这不起作用,则可能是您尝试访问父路由的参数,在这种情况下,您必须在路由器树中升级才能访问它。

    您可以使用.parent 属性执行此操作,如下所示:

    this.route.parent.params.subscribe(
      (params) => {
        console.log(params);
      }
    )
    

    根据需要多次。

    【讨论】:

    • o/p: Object {}
    • @AkshayKhale 对于两者?
    猜你喜欢
    • 1970-01-01
    • 2017-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-06
    • 1970-01-01
    • 2016-10-28
    • 2016-11-25
    相关资源
    最近更新 更多