私はいくつかの例を見てきましたが、観測可能なparamsのサブスクライブ関数を得ることができません。私はrouter.eventsを動作させることができます。私は間違って何をしていますか?パスのパラメータの変更に基づいた角4のリロードルート
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from "@angular/router";
import { ProjectService } from '../../services/project.service';
import { Project } from "../../domain/project";
@Component({
selector: 'app-nav-bar',
templateUrl: './nav-bar.component.html'
})
export class NavBarComponent implements OnInit {
projects: Project[] = [];
constructor(private projectService: ProjectService,
private route : ActivatedRoute,
private router : Router) {
this.projectService.getProjects();
}
ngOnInit() {
this.projectService.projectsChange.subscribe(result => {
this.projects = result;
});
this.projectService.projectChange.subscribe(result => {
this.projectService.getProjects();
});
// This only works the first time the component is routed to
this.activatedRoute.params.subscribe(params => {
console.log(params);
console.log("did this work at all?");
this.projectService.getProject(params['id']);
});
// This works when the param changes in the router
this.router.events.subscribe((val) => {
console.log(this.router.url);
let url = this.router.url;
let id = url.substring(url.length - 2, url.length);
this.projectService.getProject(Number(id));
});
}
}
これはこれは、リンクがテンプレート内のセットアップ
[routerLink]="['/projects/', project.id]"
this.activatedRoute.params.subscribeでエラーが発生しますか? – LLai
エラーなし、正しいURLでNavigationStart、RoutesRecognized、およびNavigationEndのみ。 – Grim
hmm、あなたのcomponent.tsコードをもう少し投稿できますか? (コンストラクタ、activatedRouteインポートなど)コンストラクタまたはngOnInitライフサイクルフックを購読していますか? – LLai