質問編集履歴
1
すいません、ソースコードを追記しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -21,3 +21,69 @@
|
|
21
21
|
|
22
22
|
|
23
23
|
しかし、何かこのやり方はダサく感じており、Angularのお作法的にはどうするのが正解なのか教えてください。
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
以下ソースコードです。
|
28
|
+
|
29
|
+
```Angualr
|
30
|
+
|
31
|
+
import { Component, OnInit } from '@angular/core';
|
32
|
+
|
33
|
+
import { ArticleService } from './article.service';
|
34
|
+
|
35
|
+
import { Article } from 'src/app/shared/models/article';
|
36
|
+
|
37
|
+
import { Router } from '@angular/router';
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
@Component({
|
42
|
+
|
43
|
+
selector: 'app-article',
|
44
|
+
|
45
|
+
templateUrl: './article.component.html',
|
46
|
+
|
47
|
+
styleUrls: ['./article.component.scss']
|
48
|
+
|
49
|
+
})
|
50
|
+
|
51
|
+
export class ArticleComponent implements OnInit {
|
52
|
+
|
53
|
+
article: Article;
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
constructor(private articleService: ArticleService,
|
58
|
+
|
59
|
+
private router: Router) { }
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
ngOnInit() {
|
64
|
+
|
65
|
+
this.getArticle(1);
|
66
|
+
|
67
|
+
}
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
getArticle(articleId: number) {
|
72
|
+
|
73
|
+
this.articleService.getArticle(articleId).subscribe(
|
74
|
+
|
75
|
+
response => {
|
76
|
+
|
77
|
+
this.article = response;
|
78
|
+
|
79
|
+
this.router.navigateByUrl('/');
|
80
|
+
|
81
|
+
}
|
82
|
+
|
83
|
+
);
|
84
|
+
|
85
|
+
}
|
86
|
+
|
87
|
+
}
|
88
|
+
|
89
|
+
```
|