我已經成功地在Angular JS中爲我的其他組件在同一個項目和新組件中實現了路由參數,但它不起作用,我無法理解代碼中的問題。無法讀取Angular 2中的路由參數
下面是我的路線的代碼文件
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
}
我錯過了什麼
在此先感謝!
同樣的問題,相同的結果 – Sajad
您是否找到任何解決方案? – Sajad