我需要在我的步进器“角度”的第一步之后附加ID

问题描述 投票:0回答:1

我需要在第一步之后在我的步进器中附加我的项目的 ID EXM: API 调用是在初始创建的第一步之后发生的,它返回一个 Id,我应该在后续步骤中使用它,但我需要在不重定向的情况下导航,我只想将 Id 附加到当前 URL,但不像查询参数那样,

我的当前路线/id

我尝试过这个,但我不希望它作为查询参数

import { Component } from '@angular/core';
import { ActivatedRoute, Router, NavigationExtras } from '@angular/router';

@Component({
  selector: 'app-your-component',
  template: `
    <!-- Your component template here -->
  `,
})
export class YourComponent {
  constructor(private route: ActivatedRoute, private router: Router) {}

  appendIdToCurrentRoute(id: string) {
    // Get the current route snapshot
    const currentRoute = this.route.snapshot;
    const currentQueryParams = { ...currentRoute.queryParams };
    currentQueryParams.id = id;

    const navigationExtras: NavigationExtras = {
      relativeTo: this.route,
      queryParams: currentQueryParams,
      queryParamsHandling: 'merge', // Keep existing query parameters
      preserveFragment: true, // Preserve existing fragment
      replaceUrl: false, // Do not replace the current URL in the browser history
    };

    // Use the router to navigate with the specified NavigationExtras
    this.router.navigate([], navigationExtras);
  }
}
angular routes rxjs
1个回答
0
投票

你能尝试做这个小改变吗?

import { Component } from '@angular/core';
import { ActivatedRoute, Router, NavigationExtras } from '@angular/router';

@Component({
  selector: 'app-your-component',
  template: `
    <!-- Your component template here -->
  `,
})
export class YourComponent {
  constructor(private route: ActivatedRoute, private router: Router) {}

  appendIdToCurrentRoute(id: string) {
    // Get the current route snapshot
    const currentRoute = this.route.snapshot;
    const currentQueryParams = { ...currentRoute.queryParams };

    const navigationExtras: NavigationExtras = {
      relativeTo: this.route,
      queryParams: currentQueryParams,
      queryParamsHandling: 'merge', // Keep existing query parameters
      preserveFragment: true, // Preserve existing fragment
      replaceUrl: false, // Do not replace the current URL in the browser history
    };

    // Use the router to navigate with the specified NavigationExtras
    this.router.navigate([id], navigationExtras);
  }
© www.soinside.com 2019 - 2024. All rights reserved.