Javascript 读取路由参数
我正在使用Paypal API,在确认购买后,它将重定向到您想要的url,我将希望的url设置为“localhost:4200/shop/order”。 但是,每当paypal返回url时,他们都会在url末尾添加令牌和payerid “本地主机:4200/商店/订单?代币=8YS089366D9655&PayerID=ASDVD4BLMH”, 然而,当它返回到我的angular应用程序时,我有一个错误,说找不到页面 我尝试了几种方法来配置路由,但所有尝试都失败了。 idk如果角度不接受路线中的“?”和“&”Javascript 读取路由参数,javascript,angular,Javascript,Angular,我正在使用Paypal API,在确认购买后,它将重定向到您想要的url,我将希望的url设置为“localhost:4200/shop/order”。 但是,每当paypal返回url时,他们都会在url末尾添加令牌和payerid “本地主机:4200/商店/订单?代币=8YS089366D9655&PayerID=ASDVD4BLMH”, 然而,当它返回到我的angular应用程序时,我有一个错误,说找不到页面 我尝试了几种方法来配置路由,但所有尝试都失败了。 idk如果角度不接受路线中的
const shopRoutingConfig:Routes = [
{
path:'',component:ShopAppComponent,
children:[
{path:'purchase',component:PurchaseComponent},
{
path:'order/:token/:payerid', //fail in url returned from paypal
component:OrderComponent
}
]
}
]
@NgModule({
imports:[
RouterModule.forChild(shopRoutingConfig)
],
exports:[RouterModule],
})
export class ShopRoutingModule{}
我的订单组件:
export class OrderComponent implements OnInit
{
constructor(private route:ActivatedRoute) {
}
ngOnInit(): void {
debugger
const routeParams = this.route.snapshot.paramMap;
var tokenText = routeParams.get('token')
var userId = routeParams.get('PayerID')
}
}
唯一有效的方法是手动编辑url以
“localhost:4200/shop/order/8DC695025P9917207”“localhost:4200/shop/order?token=8YS089366D9655&PayerID=ASDVD4BLMH”token和PayerID是查询参数,但您已将路由描述为order/:token/:PayerID,这是路由参数。
所以如果重定向URL是
“本地主机:4200/shop/order/8YS089366D9655/ASDVD4BLMH”
由于重定向URL返回queryParams,所以最好将路由设置为
path:'order/', component: yourComponent
和在组件中
constructor() {
this.route.queryParams.subscribe(params => {
this.tokenText = params.token:
this.userId = params.payerId
})
}
令牌和付款人是查询参数,您不需要将它们添加到路由器配置中,只需删除它们即可。您可以使用this.route.queryParams获取查询参数值