[英]angular 4 wrong routing url
Hi my application implements routing, I have a list of users and when I click one of it I do this: 嗨,我的应用程序实现了路由,我有一个用户列表,当我单击其中一个时,我这样做:
onRowSelect(event) {
this.self = event.data._links.self.href;
this.userId = this.url.getUserId(this.self, '/', 5);
this.router.navigate(['/dashboard/user-detail', this.userId]);
this.userId
isn't only a id code number but a string, for example: /users/5
and in the user-detail.routes I define this path: 'user-detail/:code'
this.userId
不仅是ID码,而且是字符串,例如: /users/5
并且在user-detail.routes中,我定义了以下path: 'user-detail/:code'
user-detail.component.ts user-detail.component.ts
ngOnInit() {
this.code = this.route.snapshot.params['code'];
this.getUserDetail(this.code);
}
getUserDetail(code) {
this.http.get(this.endpointUrlService.getBaseUrl() + code, this.http.setCommonOption())
.subscribe(response => {
this.selected = response.json() as User;
this.loader = 1;
});
}
The problem is that url path have the escape code '%2' for '/' like this: 问题在于,URL路径的'/'转换码为'%2',如下所示:
http://localhost:4200/dashboard/user-detail/%2Fusers%2F2
http:// localhost:4200 / dashboard / user-detail /%2Fusers%2F2
Is there a simple way to correct the url? 有没有简单的方法来纠正网址?
It is a good approach to navigate to route like 这是导航至类似路线的好方法
this.router.navigate(['dashboard', 'user-detail', 'users', userId]);
That's why navigate
takes array as a param, not a string. 这就是为什么
navigate
将数组作为参数而不是字符串的原因。 There's another method 还有另一种方法
this.router.navigateByUrl(`/'dashboard/user-detail${userId}`)
Which you can use. 您可以使用。
Edited 已编辑
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.