0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ngOnInitが動かない

Last updated at Posted at 2019-11-29

内容

 angular8でpathに
 /search/:queryと定義して:queryが変わったのに、ngOnInitが動作しない場合の対応

ダメな時のソース

ng.ts
ngOnInit() {
    q = this.route.snapshot.paramMap.get('query');
  //ここでデータを読み込む
    this.service.httpget(q).subscribe( data=>{
       this.data = data;
    }
}

動いたときのソース

ok.ts
ngOnInit() {
    this.route.paramMap.subscribe(param=>{
       q = param.get('query');
       this.service.httpget(q).subscribe( data=>{
          this.data = data;
       }
    }
}

つまり、OnInitでsubcsribeしておいて、変更を取得するということらしい。成程。なんでもsubscribeなんだなあ。デバッグ大丈夫なんかなあ?

 

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?