简体   繁体   English

Angular 6导航栏routerlink重新加载组件

[英]Angular 6 navbar routerlink reload component

Im using angular 6 and a routerlink in a dropdown menu in my navbar. 我在导航栏中的下拉菜单中使用了angular 6和routerlink。 The problem i'm facing is that routerlink isn't reloading the website and therefor not the route. 我面临的问题是routerlink不会重新加载网站,因此没有路由。 When pressing a button in the dropdown it initially loads correctly, but when the user presses another button in the dropdown (while still on another child-page of the dropdown) it doesnt reload the content on the page, but changes the URL. 当按下下拉菜单中的按钮时,它最初会正确加载,但是当用户按下下拉菜单中的另一个按钮时(仍在下拉菜单的另一个子页面上),它不会重新加载页面上的内容,而是会更改URL。 When using Href it loads everything correctly. 使用Href时,它将正确加载所有内容。

<div 
  ngbDropdownMenu 
  class="dropdown-menu" 
  aria-labelledby="navbarDropdown">
  <div *ngFor="let competentie of competenties">
    <a 
      class="dropdown-item" 
      routerlink="/competenties/{{competentie.id}}">
      {{competentie.name}}
    </a>
  </div>
</div>

This forces me to use href instead of routerlink to reload the page for the component to reload and display the content with the right {{competentie.id}}. 这迫使我使用href而不是routerlink重新加载页面,以便组件重新加载并显示具有正确的{{competentie.id}}的内容。

Is there a way to navigate to another dropdown-page with routerlink and update the content on the page? 有没有一种方法可以使用routerlink导航到另一个下拉页面并更新页面上的内容?

Edit 编辑

The details component: 详细信息组件:

export class CompetentieComponent implements OnInit {
competentie: Competentie;

constructor(private route: ActivatedRoute,
            private competentieService: CompetentieService,
            private location: Location
) {
}

getCompetentie(): void {
    const id = +this.route.snapshot.paramMap.get('id');
    this.competentieService.getCompetentie(id)
        .subscribe(competentie => this.competentie = competentie);
}

ngOnInit() {
    this.getCompetentie();
}
}

My route: 我的路线:

const routes: Routes = [
{path: 'competenties/:id', component: CompetentieComponent}
];

The service, gets it data from a class with array COMPETENTIES[]: 该服务从具有数组COMPETENTIES []的类获取数据:

export class CompetentieService {

getCompetenties(): Observable<Competentie[]> {
    return of(COMPETENTIES);
}

getCompetentie(id: number): Observable<Competentie> {
    return of(COMPETENTIES.find(competentie => competentie.id === id));
}

constructor() {}
}

You are using this.route.snapshot.paramMap.get('id') which will only trigger just once. 您正在使用this.route.snapshot.paramMap.get('id') ,它将仅触发一次。 You should be using params instead of snapshot.paramMap.get('id') . 您应该使用params而不是snapshot.paramMap.get('id') params is a BehaviorSubject subscribing to which will give you the updated id every time it changes. params是一个BehaviorSubject订阅对象,每次更改时,它都会为您提供更新的id

You should inject ActivatedRoute as a dependency in the same and then subscribe to params property on it. 您应该将ActivatedRoute注入为依赖项,然后在其上订阅params属性。

export class CompetentieComponent implements OnInit {
  competentie: Competentie;

  constructor(private route: ActivatedRoute,
    private competentieService: CompetentieService,
    private location: Location
  ) {}

  getCompetentie(): void {
    this.activatedRoute.params.subscribe(params => {
      let latestID = +params['id'];
      this.competentieService.getCompetentie(latestID)
        .subscribe(competentie => this.competentie = competentie);
    });
  }

  ngOnInit() {
    this.getCompetentie();
  }

}

Try the following for dynamic routes: 对于动态路由,请尝试以下操作:

<a class="dropdown-item"  [routerLink]="['/competenties/' + competentie.id]">
  {{competentie.name}}
</a>

add this to your code: 将此添加到您的代码:

<a class="dropdown-item"  [routerLink]="['/competenties/' + competentie.id]">
  {{competentie.name}}
</a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM