我试图在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没有给我当前活动的http://localhost:4200/core/enterpriseprofile
。它提供了这个Route(url:'', path:'')
空对象。
答案 0 :(得分:0)
this.route
代表ActivatedRoute。要从相同网址获取网址,可以使用ActivatedRoute的url
属性。
由于url
属性是可观察的,因此您需要对其进行一些操作以将其作为字符串获取,例如:
const url: Observable<string> = this.route.url.pipe(map(segments => segments.join('/')));
现在,您可以像下面一样订阅url
以获得相对网址:
this.url.subscribe(value=>{
console.log(value);
});
答案 1 :(得分:0)
如果您需要当前路线,则必须使用const admin = [{
"categoryId": 66,
"categoryName": "category 66",
"id": 204
},
{
"categoryId": 149,
"teamName": "category 149",
"id": 178
}
]
const member = [{
"categoryId": 66,
"teamName": "category 66",
"id": 271
},
{
"categoryId": 68,
"teamName": "category 68",
"id": 264
}
]
const findCategory = (categories, category) => categories
.find(x => x.categoryId === category) || null;
const mergeCategories = (adminIn, memberIn) => {
const mergedCategories = [
...adminIn,
...memberIn,
];
// we would like to know just the unique id's
const categoryIds = [...new Set(mergedCategories.map(x => x.categoryId))];
// we can map each unique id
return categoryIds.map(categoryId => {
// then we can find whether it has a matching category for each
const adminCategory = findCategory(admin, categoryId);
const memberCategory = findCategory(member, categoryId);
// now we can use that data to build the object in the required format
return {
categoryId,
categoryName: `category ${categoryId}`,
adminId: adminCategory === null ? adminCategory : adminCategory.id,
memberId: memberCategory === null ? memberCategory : memberCategory.id
}
});
}
const result = mergeCategories(
admin,
member,
)
console.dir(result)
有关更多选项,请检查this.router.url
对象。这将为您提供很多选择。
示例
window.location