Angular routerlink 使用分号而不是问号,url 未按预期运行

Angular routerlink using semicolon instead of question mark, url not behaving as expected

我的代码中有以下按钮:

<button [routerLink]="['/answerslist', {id: applicant.id} ]" label="View Interview"></button> 

对应以下路线:

{ path: 'answerslist', component: AnswersListComponent },

我期待 URL 出现如下情况: http://localhost:4200/answerslist?id=member2

但是我在点击按钮时得到了这个: http://localhost:4200/answerslist;id=member2

(分号代替问号)

因此,代码无法正常运行。我正在检索参数以备后用,如下所示:

  getUser(){
    this.activeRoute.queryParams.subscribe(
      (params) => {
        const id = params['id'];
        if (id) { 
          this.currentUser = id;
        }
        else{
          console.log('id not passed'); //this is what currenlty gets printed
        }
      });
  }

所以我需要一种方法来构造带有问号而不是分号的 URL

请试试这个

< button [routerLink]="['answerslist']" [queryParams]="{ id: applicant.id }"></button >

可以在 https://angular.io/api/router/RouterLink

上找到更多信息