我有这个路由
{
path: ':lang',
children: [
{
path: 'page',
children: [
{
path: '',
component: PageComponent
},
{
path: ':Id',
component: PageComponent
}
]
},
...
}];
我的组件如下所示:
export class PageComponent implements OnInit {
busy: any;
pages: Page[];
src: any;
constructor(private httpCall: HttpCall, private router: Router, private activedRoute: ActivatedRoute, public languageService: LanguageServiceService, private http: Http) {
}
ngOnInit() {
let Id = this.activedRoute.snapshot.params['Id'];
this.busy = this.httpCall.get('/pub/page/GetPageById/' + Id)
.subscribe(
data => {
this.pages = <Page[]>data;
})
}
}
当我第一次访问时,例如
domain.com/en/page/13
它正常重定向,但如果我尝试重定向到
domain.com/en/page/14
从那里改变网址,但在页面内部,一切都保持不变。
我认为这是因为ngOnInit
仅呈现一次,直到我进入另一个网址(页面外)
我该如何解决?
答案 0 :(得分:0)
快照将返回单个值,因此您必须订阅“this.activedRoute.params”可观察量,该值可在每次路径“domain.com/en/page/”更改时解析。
import {Params} from "@angular/router";
export class PageComponent implements OnInit {
busy: any;
pages: Page[];
src: any;
constructor(private httpCall: HttpCall, private router: Router, private activedRoute: ActivatedRoute, public languageService: LanguageServiceService, private http: Http) {
}
ngOnInit() {
this.activedRoute.params.subscribe( (params: Params)=>{
let iD = params['Id'];
this.busy = this.httpCall.get('/pub/page/GetPageById/' + Id)
.subscribe(
data => {
this.pages = <Page[]>data;
})
});
}