Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无法读取角度2中的路由参数_Javascript_Angular_Angular2 Routing_Angular Routing - Fatal编程技术网

Javascript 无法读取角度2中的路由参数

Javascript 无法读取角度2中的路由参数,javascript,angular,angular2-routing,angular-routing,Javascript,Angular,Angular2 Routing,Angular Routing,我已经成功地在Angular JS中为同一项目中的其他组件实现了route参数,对于新组件,我也遵循同样的方法,但它不起作用,我无法理解代码中的问题 下面是我的路由文件的代码 import { Routes, RouterModule } from '@angular/router'; import { CustomerBillComponent } from '../components/customer_bill.component'; const routes: Routes = [

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

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

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文件中的代码

我错过什么了吗


提前感谢

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

import {ModuleWithProviders} from '@angular/core';

export const routing: ModuleWithProviders = RouterModule.forRoot(router);
第一次尝试:

this.route.params.subscribe(
  (params) => {
    console.log(params);
  }
)
看看你能得到什么。如果这不起作用,很可能您试图访问父路由的参数,在这种情况下,您必须在路由器树中升级才能访问它

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

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

你需要多少次都行。

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

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

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

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 { }
ngOnInit() {
    this.sub = this.route.params.subscribe(params => {
        this.routeText = params['category'];
        this.title = this.formatTitle(this.routeText);
    });
}
const routeChildren = this.activatedRoute.snapshot.children;
if (routeChildren.length) {
    //Your code here to assign route params to the Component's Data Member.
}
因此,我必须访问HomeComponent(父级)中的RouteParam值,借出页面是BuyComponent(HomeComponent的子路由)

因此,我没有使用以下代码获取route params的值:

//内部HomeComponent.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 { }
ngOnInit() {
    this.sub = this.route.params.subscribe(params => {
        this.routeText = params['category'];
        this.title = this.formatTitle(this.routeText);
    });
}
const routeChildren = this.activatedRoute.snapshot.children;
if (routeChildren.length) {
    //Your code here to assign route params to the Component's Data Member.
}
由于借出页面是BuyComponent,因此上述代码将不起作用。Angular允许仅在着陆组件上以上述方式访问route params

我做了很多尝试,最终找到了一个在父路由组件中访问路由参数的解决方案

下面是访问相同文件的代码:

//内部HomeComponent.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 { }
ngOnInit() {
    this.sub = this.route.params.subscribe(params => {
        this.routeText = params['category'];
        this.title = this.formatTitle(this.routeText);
    });
}
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
为我实现了这个技巧

希望这将有助于如果有人在这里寻找像我这样的问题


致意。

请填写您的路线文件。TyI尝试过,但客户参考仍
未定义
谢谢帮助…您确定已在构造函数中声明路由吗?我认为您应该使用paramMap而不是params。this.route.snapshot.paramMap.get('customer_reference');在我的构造函数
private route:ActivatedRoute
中,当我将Param改为paramMap时,它说属性paramMap不存在于ActiveRouteSnapshoto/p:
Object{}
@AkshayKhale用于这两者?同样的问题,同样的结果找到了解决方案吗?您可能需要使用
paramMap
而不是
params
。ie:
this.route.snapshot.paramMap.get('customer\u reference')
这对我很有效