【发布时间】: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