使用 routerLink Angular 传递不可见或隐藏的参数

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

我的路由器链接如下:

<button class="take-a-tour-btn" [routerLink]="['/dashboard', {'showTour':'show'}]">

我想传递参数

showTour
。但是,当我这样做时,参数在
url
中可见,我想隐藏它。我已经阅读了很多参考资料(其中提到了可选参数),但在我的案例中没有成功。我该如何解决这个问题?

angular router routeparams
7个回答
39
投票

您可以使用 历史状态 将动态数据传递到您想要导航到的组件,而无需将它们添加到 URL 中,如下所示:

this.router.navigateByUrl('/user', { state: { orderId: 1234 } });

<a [routerLink]="/user" [state]="{ orderId: 1234 }">Go to user's detail</a>

你可以这样得到它

constructor() {
  this.router.events
   .pipe(filter(e => e instanceof NavigationStart))
   .subscribe((e: NavigationStart) => {
    const navigation  = this.router.getCurrentNavigation();
    this.orderId = navigation.extras.state ? navigation.extras.state.orderId : 0;
   });
 }

17
投票

我不确定是否有办法做到这一点,因为数据需要在 URL 字符串中呈现。

我的建议是使用全球服务来存储所需的数据。例如:

//some dataService, which store your needed data.
@Injectable()
export class DataService {

   _showTour: string;

   set showTour(value: string) {
      this._showTour = value;
   }

   get showTour(): string {
       return this._showTour;
   }

   constructor() {}

}

并以这种方式在导航组件中使用它:

//your navigation component
@Component({
    selector: 'my-app',
    template: `
       <button class="take-a-tour-btn" (click)="onClick()">
    `
})
export class SomeComponent {
    constructor(private dataService: DataService, private router: Router) { }

    onClick() {
        this.dataService.showTour = 'show';
        this.router.navigate(['/dashboard']);
    }
}

您可以在仪表板组件中使用相同的服务,并获得所需的值,例如这样:

//your dashboard component
@Component({
    selector: 'my-dashboard',
    template: `
       <h1>{{ showTour }}</h1>
    `
})
export class DashboardComponent implements OnInit {

    showTour: string;

    constructor(private dataService: DataService) { }

    ngOnInit() {
        this.showTour = this.dataService.showTour;
    }
}

9
投票

使用状态传递隐藏参数和历史记录来读取它们。

第一个组件:

this.router.navigate(
      [`/dashboard/roles/${id}`],
      { state: { navSettings: navSettings } });

第二部分:

public ngOnInit(): void {
    const id = this.activatedRoute.snapshot.params.id;
    this.initNavSettings(history.state.navSettings);
}

4
投票
<button class="take-a-tour-btn" [routerLink]="['/dashboard', {'showTour':'show', skipLocationChange: true}]">

尝试使用skipLocationChange属性。


2
投票

在你的组件中尝试一下:(我将其与 Angular 5.x 一起使用)

this.router.navigate(['/dashboard'], {skipLocationChange: true, replaceUrl: false});

0
投票

@AddWeb @SailiUnique

您的解决方案有效,但您不必将属性放入 [routLink] 中。正确的位置是在它的外面,就像任何财产一样。

像这样:

<div class="row inline rowItem" [routerLink]="['/businessPartnerDetails']" [queryParams]="{ id: bpItem.id, bp: bpItem.companyName}" skipLocationChange=true (click)="onViewDetails(bpItem)">


0
投票

响应并不完全有效,因为skipLocationChange不会更改浏览器url上的当前路线,如果您想稍后返回,则可以从第一条路线返回。

例如,如果您在主页上并使用此:

<button class="take-a-tour-btn" [routerLink]="['/dashboard', {'showTour':'show', skipLocationChange: true}]">Go to dashboard</button>

如果您想从

dashboard
返回
home
,则无法使用对象
location
执行此操作,在这种情况下,您需要使用
Router
并指定准确的路线 (
/dashboard
)。

但是在很多情况下这是一个糟糕的解决方案,并且浏览器路由不会从

/home
更改为
dashboard

不方便

  • 浏览器未刷新到正确的网址
  • 您无法从
    dashboard
    (当前路线)返回

您可以创建数据服务或查看官方Angular Router文档

© www.soinside.com 2019 - 2024. All rights reserved.