angular - RouterService 中的 this.route 没有正确的 url
问题描述
我试图在我的中编写一个函数RouterService
来添加查询参数。这是我的路由器服务
export class RouterService {
constructor(
private router : Router,
private route: ActivatedRoute
) { }
addQueryParams(params : {}){
debugger;
console.log(this.route.toString());
this.router.navigate([], {
relativeTo: this.route,
queryParams: params,
queryParamsHandling: 'merge',
skipLocationChange: true
});
}
}
在此处添加是我正在使用此功能的组件。
navegateRole(){
this.routerService.addQueryParams({id : "2"});
}
现在,在我的 RouterService 中 this.route 没有给我当前的 active http://localhost:4200/core/enterpriseprofile
。它给出了这个Route(url:'', path:'')
空对象。
解决方案
this.route
在您的代码中代表ActivatedRoute。要从中获取 url,您可以使用url
ActivatedRoute 的属性。
由于url
属性是可观察的,您需要对其执行一些操作以将其作为字符串获取,例如:
const url: Observable<string> = this.route.url.pipe(map(segments => segments.join('/')));
现在,您可以url
像下面这样订阅以获取相对 url:
this.url.subscribe(value=>{
console.log(value);
});
推荐阅读
- ios - 在 iOS 中的指定时间运行代码有哪些不同的选项?
- cucumber-java - 如何阻止 Cucumber runner 在控制台中打印详细信息
- sql - 插入具有相同架构的不同列的表总和
- django - django 2.1 无法删除项目
- javascript - 在jQuery中向正文添加类不起作用
- json - 快速解析 JSON 数据
- python - 如何替换熊猫中的数据框
- html - Tumblr 标题和提要间距
- python - PYTHON 中的错误:TypeError:ufunc 'multiply' 不包含签名匹配类型 dtype('
我在 python 中运行这段代码
with open("test.txt", "r") as networkFile: for line in networkFile: src = line.split(
- node.js - SailsJS - 永远加载路线