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

我有如下路由器链接
<button class="take-a-tour-btn" [routerLink]="['/dashboard',{'showTour':'show'}]">

我想传递参数showTour.但是,当我这样做时,参数在url中可见,我想隐藏它.我经历了这么多的引用(关于可选参数的说法),但在我的案例中没有成功.我该怎么解决这个问题?

我不确定,是否有办法,因为数据需要在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;
    }
}

相关文章

ANGULAR.JS:NG-SELECTANDNG-OPTIONSPS:其实看英文文档比看中...
AngularJS中使用Chart.js制折线图与饼图实例  Chart.js 是...
IE浏览器兼容性后续前言 继续尝试解决IE浏览器兼容性问题,...
Angular实现下拉菜单多选写这篇文章时,引用文章地址如下:h...
在AngularJS应用中集成科大讯飞语音输入功能前言 根据项目...
Angular数据更新不及时问题探讨前言 在修复控制角标正确变...