我需要在一个组件中检测路径参数和查询参数的url更改。
{ path: 'category/:key', component: CollectionPageComponent},
Run Code Online (Sandbox Code Playgroud)
最后的url将是例如category / t-shirt或category / t-shirt?page = 2 我需要基于categoryLink获取所有category的产品并进行分页。
我使用CombineLatest
combineLatest(this.route.params, this.route.queryParams)
.pipe(map(results => ({params: results[0].key, query: results[1]})))
.subscribe(results => {
console.log("run me");
console.log(results);
});
Run Code Online (Sandbox Code Playgroud)
一切正常,除了一种情况,它将像我的演示链接一样运行两次https://angular-ev2rns-subscribe-param-and-path-param.stackblitz.io
首先单击cat1,然后单击page1-> fire one。第二次单击cat2->将触发两次
然后我找到了其他解决方案
ngOnInit() {
// I have to handle it first time
this.categoryKey = this.activatedRoute.snapshot.params.key;
let page = this.activatedRoute.snapshot.queryParams.page;
// call service get data
this.sub = this.router.events.pipe(
filter((event) => event instanceof NavigationEnd)
)
.subscribe((event:NavigationEnd) => {
console.log("run me "); …Run Code Online (Sandbox Code Playgroud) 我已经使用 ng-bootstrap https: // ng-bootstrap https://ng-bootstrap.github.io/#/components/carousel/examples#navigation在角度版本 7 中显示轮播。我想获取图像的当前索引。我需要它来显示与图像对应的背景颜色。然后我看到一个事件(幻灯片),它向我展示了一些我可以用来确定图像索引的元信息。(我总共有3张图片)
{"prev":"ngb-slide-1","current":"ngb-slide-2","direction":"left"}
Run Code Online (Sandbox Code Playgroud)
当我导航到其他页面并返回时的一个问题。元信息从 ngb-slide-4 而不是 ngb-slide-0 开始。
在此处查看我的链接演示https://angular-ndxybr.stackblitz.io/
回到我的问题,如何在滑动时获取图像的索引?非常感谢。
我可以通过从 ngb-slide-2 或 ngb-slide-5(下次)中提取数字来解决。index = number % 3,但如果图片的数量发生变化则不正确
angular ×2