回答編集履歴
5
追加コメントに対する回答
answer
CHANGED
@@ -1,3 +1,15 @@
|
|
1
|
+
コメントに関してご回答します。
|
2
|
+
|
3
|
+
>DIするサービスの変数はそれぞれのコンポーネントで、共有できるという解釈でいいものでしょうか?
|
4
|
+
|
5
|
+
基本的にその解釈で正しいです。ドキュメントを見ると、「CLIで生成されたアプリケーションでは、モジュールは事前ロードされプロバイダー(service)は全モジュールで利用可能となる」と有ります。そして「Angularルーターがモジュールを遅延ロードすると、新しいインジェクターが作成されます」とあり、遅延ロードしたComponentでは別インスタンスとなり共有できないものと解釈できます。
|
6
|
+
|
7
|
+
[https://angular.jp/guide/providers](https://angular.jp/guide/providers)
|
8
|
+
|
9
|
+
angularではページ部品をcomponentとすることでcomponentを組み合わせてページを作成したり、他ページで再利用することが可能です。そしてcomponent間のデータ受け渡しやイベントの通知にserviceをDIしてsubscribeを使用する方法が有ります。また、DB連動のためにrestサービスを使用する場合なども、serviceとして実装していれば、メソッドを各componentで共通利用できます。単にデータ共有する場合ではsubscribeを利用する必要は有りませんが、自componentで発生したイベントを他componentに通知したい時にはsubscribeを利用することになります。
|
10
|
+
|
11
|
+
---
|
12
|
+
|
1
13
|
実際に画面遷移するコードを書いて試してみました。結果、Routerを使用した画面遷移を行えばserviceは共有でき、serviceに保持用のメンバを配置してget/setterを使えばページ間のデータ受け渡しが出来ました。また、subscribeではデータ送信時(next)に受信側のComponentのインスタンスが不在なのでデータは受け取れないようです。ご参考までに確認したコードを記載します。
|
2
14
|
|
3
15
|
app.module.ts
|
4
service記載漏れの補完
answer
CHANGED
@@ -127,6 +127,30 @@
|
|
127
127
|
}
|
128
128
|
|
129
129
|
```
|
130
|
+
test.service.ts
|
131
|
+
```javascript
|
132
|
+
import { Injectable } from '@angular/core';
|
133
|
+
|
134
|
+
@Injectable({
|
135
|
+
providedIn: 'root'
|
136
|
+
})
|
137
|
+
|
138
|
+
export class TestService {
|
139
|
+
|
140
|
+
private message:string;
|
141
|
+
|
142
|
+
getMessage():string{
|
143
|
+
return this.message;
|
144
|
+
}
|
145
|
+
|
146
|
+
setMessage(msg:string){
|
147
|
+
this.message=msg;
|
148
|
+
}
|
149
|
+
|
150
|
+
}
|
151
|
+
|
152
|
+
```
|
153
|
+
|
130
154
|
---
|
131
155
|
console.logの表示が正しいのであれば、subscribeのcallbackは出来ていると思えます。そうなるとComponent(class)とtemplate(html)のマッピングがどこかおかしいのではないでしょうか。
|
132
156
|
|
3
検証結果の報告
answer
CHANGED
@@ -1,5 +1,133 @@
|
|
1
|
-
|
1
|
+
実際に画面遷移するコードを書いて試してみました。結果、Routerを使用した画面遷移を行えばserviceは共有でき、serviceに保持用のメンバを配置してget/setterを使えばページ間のデータ受け渡しが出来ました。また、subscribeではデータ送信時(next)に受信側のComponentのインスタンスが不在なのでデータは受け取れないようです。ご参考までに確認したコードを記載します。
|
2
2
|
|
3
|
+
app.module.ts
|
4
|
+
```javascript
|
5
|
+
import { BrowserModule } from '@angular/platform-browser';
|
6
|
+
import { NgModule } from '@angular/core';
|
7
|
+
|
8
|
+
import { AppRoutingModule } from './app-routing.module';
|
9
|
+
import { AppComponent } from './app.component';
|
10
|
+
|
11
|
+
import { SendComponent } from './send/send.component';
|
12
|
+
import { ReceiveComponent } from './receive/receive.component';
|
13
|
+
import {TestService} from './test.service'
|
14
|
+
|
15
|
+
@NgModule({
|
16
|
+
declarations: [
|
17
|
+
AppComponent,
|
18
|
+
SendComponent,
|
19
|
+
ReceiveComponent
|
20
|
+
],
|
21
|
+
imports: [
|
22
|
+
BrowserModule,
|
23
|
+
AppRoutingModule
|
24
|
+
],
|
25
|
+
providers: [TestService],
|
26
|
+
bootstrap: [AppComponent]
|
27
|
+
})
|
28
|
+
export class AppModule { }
|
29
|
+
|
30
|
+
```
|
31
|
+
app-routing.module.ts
|
32
|
+
```javascript
|
33
|
+
import { NgModule } from '@angular/core';
|
34
|
+
import { Routes, RouterModule } from '@angular/router';
|
35
|
+
|
36
|
+
import { SendComponent } from './send/send.component';
|
37
|
+
import { ReceiveComponent } from './receive/receive.component';
|
38
|
+
|
39
|
+
const routes: Routes = [
|
40
|
+
{path:'send',component:SendComponent},
|
41
|
+
{path:'receive',component:ReceiveComponent}
|
42
|
+
];
|
43
|
+
|
44
|
+
@NgModule({
|
45
|
+
imports: [RouterModule.forRoot(routes)],
|
46
|
+
exports: [RouterModule]
|
47
|
+
})
|
48
|
+
export class AppRoutingModule { }
|
49
|
+
```
|
50
|
+
app.component.ts
|
51
|
+
```javascript
|
52
|
+
import { Component,OnInit } from '@angular/core';
|
53
|
+
import { Router } from '@angular/router';
|
54
|
+
|
55
|
+
@Component({
|
56
|
+
selector: 'app-root',
|
57
|
+
templateUrl: './app.component.html',
|
58
|
+
styleUrls: ['./app.component.css']
|
59
|
+
})
|
60
|
+
export class AppComponent implements OnInit{
|
61
|
+
title = 'tera01';
|
62
|
+
|
63
|
+
constructor(private router:Router) { }
|
64
|
+
|
65
|
+
ngOnInit(){
|
66
|
+
this.router.navigate(['send']);
|
67
|
+
}
|
68
|
+
|
69
|
+
}
|
70
|
+
|
71
|
+
```
|
72
|
+
|
73
|
+
app.component.html
|
74
|
+
```html
|
75
|
+
<router-outlet></router-outlet>
|
76
|
+
```
|
77
|
+
send.component.ts
|
78
|
+
```javascript
|
79
|
+
import { Component, OnInit } from '@angular/core';
|
80
|
+
import { Router } from '@angular/router';
|
81
|
+
|
82
|
+
import {TestService} from '../test.service'
|
83
|
+
|
84
|
+
@Component({
|
85
|
+
selector: 'app-send',
|
86
|
+
templateUrl: './send.component.html',
|
87
|
+
styleUrls: ['./send.component.css']
|
88
|
+
})
|
89
|
+
export class SendComponent implements OnInit {
|
90
|
+
|
91
|
+
constructor(private service:TestService,private router:Router) { }
|
92
|
+
|
93
|
+
ngOnInit() {
|
94
|
+
}
|
95
|
+
|
96
|
+
onClick(){
|
97
|
+
this.service.setMessage("from SendComponent");
|
98
|
+
this.router.navigate(['receive']);
|
99
|
+
}
|
100
|
+
|
101
|
+
|
102
|
+
}
|
103
|
+
|
104
|
+
```
|
105
|
+
receive.component.ts
|
106
|
+
```javascript
|
107
|
+
import { Component, OnInit } from '@angular/core';
|
108
|
+
|
109
|
+
import {TestService} from '../test.service'
|
110
|
+
|
111
|
+
@Component({
|
112
|
+
selector: 'app-receive',
|
113
|
+
templateUrl: './receive.component.html',
|
114
|
+
styleUrls: ['./receive.component.css']
|
115
|
+
})
|
116
|
+
export class ReceiveComponent implements OnInit {
|
117
|
+
|
118
|
+
msg:any;
|
119
|
+
|
120
|
+
constructor(private service:TestService) {
|
121
|
+
}
|
122
|
+
|
123
|
+
ngOnInit() {
|
124
|
+
this.msg = this.service.getMessage();
|
125
|
+
}
|
126
|
+
|
127
|
+
}
|
128
|
+
|
129
|
+
```
|
130
|
+
---
|
3
131
|
console.logの表示が正しいのであれば、subscribeのcallbackは出来ていると思えます。そうなるとComponent(class)とtemplate(html)のマッピングがどこかおかしいのではないでしょうか。
|
4
132
|
|
5
133
|
@Componentの情報は記載できますか?
|
2
思い違いについて記述
answer
CHANGED
@@ -8,4 +8,8 @@
|
|
8
8
|
|
9
9
|
<追記>
|
10
10
|
this.aaaa = event; を this.aaaa = "test";と変えてみてブラウザ表示されるのであればtemplateとのマッピングは解決している(領域は共有している)と思います。bbbbでも成功しているようですし。そうなると引数で渡ってくるeventの内容(もしくは型)に表示できない要素があるのではと推測します。
|
11
|
-
aaaa:anyと宣言してもNGだったでしょうか?
|
11
|
+
aaaa:anyと宣言してもNGだったでしょうか?
|
12
|
+
|
13
|
+
<追記>
|
14
|
+
すみません、初めから勘違いをしていたようです。画面遷移しているのですね(読み落としておりました)。SPAデザインと思いこんでいました。遷移先画面ではserviceをinjectしても別インスタンスになるものと思われます(試していないのですが恐らくそうでしょう)そうなると遷移前画面からのsubscribは読めないですね。前述の「領域は共有している」は撤回いたします。遷移先ページには他手段で値を渡す必要があると思います。
|
15
|
+
「angular 画面遷移 データ受け渡し」で検索されてはどうでしょうか。
|
1
試行経過を見た思い付き
answer
CHANGED
@@ -4,4 +4,8 @@
|
|
4
4
|
|
5
5
|
@Componentの情報は記載できますか?
|
6
6
|
|
7
|
-
aaaa以外に変数を定義し、templateと双方向バインドが出来ているか確認できますか?
|
7
|
+
aaaa以外に変数を定義し、templateと双方向バインドが出来ているか確認できますか?
|
8
|
+
|
9
|
+
<追記>
|
10
|
+
this.aaaa = event; を this.aaaa = "test";と変えてみてブラウザ表示されるのであればtemplateとのマッピングは解決している(領域は共有している)と思います。bbbbでも成功しているようですし。そうなると引数で渡ってくるeventの内容(もしくは型)に表示できない要素があるのではと推測します。
|
11
|
+
aaaa:anyと宣言してもNGだったでしょうか?
|