鍍金池/ 問(wèn)答/HTML5  HTML/ angular2跳轉(zhuǎn)路由沒(méi)有觸發(fā)ngOninit生命周期函數(shù)?

angular2跳轉(zhuǎn)路由沒(méi)有觸發(fā)ngOninit生命周期函數(shù)?

在另一個(gè)路由中是這樣跳轉(zhuǎn)的:

_this.router.navigate(['/examdetail'], { queryParams: { examid: _this.examId[dataIndex]}});

跳到examdetail這個(gè)路由的時(shí)候只觸發(fā)了constructor,ngOnInit沒(méi)有觸發(fā):

  ngOnInit() {
    console.log(222);
  }
  
當(dāng)重新刷新這個(gè)頁(yè)面的時(shí)候會(huì)觸發(fā)oninit,但是我想在已進(jìn)入這個(gè)路由的時(shí)候觸發(fā),該怎么做,還有就是為什么會(huì)導(dǎo)致觸發(fā)不到oninit這個(gè)生命周期函數(shù)?
回答
編輯回答
風(fēng)畔

你肯定是在examdetail 這個(gè)同一個(gè)路由下進(jìn)行跳轉(zhuǎn)

這是因?yàn)锳ngular在同一個(gè)頁(yè)面內(nèi)跳轉(zhuǎn)時(shí),不會(huì)重新創(chuàng)建組件的實(shí)例。如樓上所說(shuō),可以通過(guò)監(jiān)聽(tīng)ActiveRoute的params事件進(jìn)行跳轉(zhuǎn)。

2018年2月8日 01:56
編輯回答
心悲涼

可以直接監(jiān)聽(tīng)ActiveRoute的params事件

constructor(private _route: ActivatedRoute,) {
    this._route.params.subscribe((params: Params) => {
        //params: {examid: 你傳入的值}
    }
}

2017年11月20日 09:59
編輯回答
還吻

我也出現(xiàn)了這個(gè)問(wèn)題,實(shí)例化之后路由跳轉(zhuǎn),就會(huì)這樣

2018年6月28日 04:29