質問編集履歴
1
すいません、ソースコードを追記しました。
title
CHANGED
File without changes
|
body
CHANGED
@@ -9,4 +9,37 @@
|
|
9
9
|
|
10
10
|
次にサーバにリクエストを投げる関数の最後で@angular/routerのnavigateByUrlを使い、同画面を再表示するようにすることで、正しく記事の内容を表示できました。
|
11
11
|
|
12
|
-
しかし、何かこのやり方はダサく感じており、Angularのお作法的にはどうするのが正解なのか教えてください。
|
12
|
+
しかし、何かこのやり方はダサく感じており、Angularのお作法的にはどうするのが正解なのか教えてください。
|
13
|
+
|
14
|
+
以下ソースコードです。
|
15
|
+
```Angualr
|
16
|
+
import { Component, OnInit } from '@angular/core';
|
17
|
+
import { ArticleService } from './article.service';
|
18
|
+
import { Article } from 'src/app/shared/models/article';
|
19
|
+
import { Router } from '@angular/router';
|
20
|
+
|
21
|
+
@Component({
|
22
|
+
selector: 'app-article',
|
23
|
+
templateUrl: './article.component.html',
|
24
|
+
styleUrls: ['./article.component.scss']
|
25
|
+
})
|
26
|
+
export class ArticleComponent implements OnInit {
|
27
|
+
article: Article;
|
28
|
+
|
29
|
+
constructor(private articleService: ArticleService,
|
30
|
+
private router: Router) { }
|
31
|
+
|
32
|
+
ngOnInit() {
|
33
|
+
this.getArticle(1);
|
34
|
+
}
|
35
|
+
|
36
|
+
getArticle(articleId: number) {
|
37
|
+
this.articleService.getArticle(articleId).subscribe(
|
38
|
+
response => {
|
39
|
+
this.article = response;
|
40
|
+
this.router.navigateByUrl('/');
|
41
|
+
}
|
42
|
+
);
|
43
|
+
}
|
44
|
+
}
|
45
|
+
```
|