亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我如何在角度中找到先前路線的參數

我如何在角度中找到先前路線的參數

慕婉清6462132 2022-10-13 10:31:31
我想在 angular typescript 中找到先前路線中的參數。我使用此代碼:private previousUrl: string = undefined;private currentUrl: string = undefined;constructor(private router: Router) {    this.currentUrl = this.router.url;    router.events.subscribe(event => {        if (event instanceof NavigationEnd) {            this.previousUrl = event.url;            this.currentUrl =  this.currentUrl;        }    });}但我無法訪問此網址的參數:http://localhost:4200/claims-manager/200/edit我想要 ti 訪問200。我怎樣才能在 url 中找到參數????
查看完整描述

3 回答

?
慕少森

TA貢獻2019條經驗 獲得超9個贊

您可以在組件文件中執行此操作,但最佳實踐是在服務中執行此操作(使用 rxjs)以傳遞數據并在組件文件中調用它


為您服務


export class myService  {   

  constructor() { } 

  private param = new BehaviorSubject("");

  sharedParam = this.param.asObservable();



  paramToPass(param:string) { 

    this.param.next(param)}    

}

在設置參數的組件類中


export class ComponentSetParam  {

 param: string   

    constructor(private myService: Service)

  

 this.myService.setParam(this.param);

}


在你的appModule


@NgModule({

  declarations: [YourComponents]

  imports: [ AppRoutingModule, YourModules...],

  providers: [ShareService],

})

export class AppModule {}

要傳遞數據的組件


export class ComponentGetParam  {

    paramFromService: string

    

     constructor(private myService: Service) {

       this.shareService.sharedData.subscribe(data : string => { 

         this.paramFromService = data;

     })

   }

  


}


查看完整回答
反對 回復 2022-10-13
?
紅顏莎娜

TA貢獻1842條經驗 獲得超13個贊

演示您可以在服務中進行


import { Injectable } from '@angular/core';

import { BehaviorSubject } from 'rxjs';

@Injectable()

export class ShareService  {   

  constructor() { } 

  private paramSource = new BehaviorSubject("");

  sharedData = this.paramSource.asObservable();

  setParam(param:string) { this.paramSource.next(param)}    

}

在構造函數中


constructor(private shareService: ShareService)

在組件中ngOnDestroy設置這樣的 this.shareService.setParam(param);


在應用模塊中


  providers:[ShareService ]

在 ngOnInit 或構造函數中的新組件中得到類似


 this.shareService.sharedData.subscribe(data=> { console.log(data); }) 


查看完整回答
反對 回復 2022-10-13
?
料青山看我應如是

TA貢獻1772條經驗 獲得超8個贊

嘗試這個:


readonly _destroy$: ReplaySubject<boolean> = new ReplaySubject<boolean>(1);


constructor(

    private activatedRoute: ActivatedRoute,

) {

  this.activatedRoute.parent.paramMap

      .pipe(

        distinctUntilChanged(),

        takeUntil(this._destroy$)

        )

      .subscribe((params: ParamMap) => {

        const id = params.get('id');

    });

}


ngOnDestroy() {

  this._destroy$.next(true);

  this._destroy$.complete();

}

其中“id”是您在路由中使用的名稱,例如


path: '/claims-manager/:id/'


查看完整回答
反對 回復 2022-10-13
  • 3 回答
  • 0 關注
  • 103 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號