簡體   English   中英

Angular 2.調用組件和獲取參數只工作一次

[英]Angular 2. Calling a component and getting the parameter works one time only

我有這樣定義的路線:

{
        path: 'manageagreements', component: ManageagreementsComponent,
        children: [
          { path: 'editagreement/:agreement', component: EditagreementComponent },
        ]
      }

我試圖獲得參數協議,我用下面的代碼做得很好:

ngOnInit(): void {
    this.activatedRoute.params.subscribe(parameter => {
      this.agreement = parameter.agreement;
    })
    alert(this.agreement)
  }

在 manageagreements.component 我有兩個按鈕,用於傳遞參數 tos 和 pp。

當我第一次從其父組件調用組件時,這工作正常,如下所示:

http://localhost:4200/platform/manageagreements/editagreement/tos

但是當它已經被渲染時,我從父組件中調用它,使用不同的參數,它就不起作用了:

http://localhost:4200/platform/manageagreements/editagreement/pp

我不知道我做錯了什么,我會感謝你的幫助。

謝謝。

如果有人正在尋找此問題的解決方案,您可以直接在組件級別更改 routeReuseStrategy:

this.router.routeReuseStrategy.shouldReuseRoute = () => false;

所以代碼最終是這樣的(對於上面的例子):

public agreement: string;
  constructor(
    private activatedRoute: ActivatedRoute,
    private router: Router,
  ) { 
    this.router.routeReuseStrategy.shouldReuseRoute = () => false;
    this.activatedRoute.params.subscribe((params: Params) => {
      this.agreement = params.agreement;
    });
    alert(this.agreement)
  }

謝謝。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM