Pārlūkot izejas kodu

added provider api

Nhu Nguyen 6 gadus atpakaļ
vecāks
revīzija
824a0d721d

+ 35
- 0
README.md Parādīt failu

@@ -0,0 +1,35 @@
1
+This is a simple ionic app to show you how to use provider to make request to the server.
2
+
3
+You can generate your own provider with the command
4
+
5
+```
6
+ionic g provider [providername]
7
+```
8
+
9
+This will create a `providers/[providername]/[providername].ts` file. In that file you will make the call to your server. You can read [Accessing Rest With Angular](https://medium.com/codingthesmartway-com-blog/angular-4-3-httpclient-accessing-rest-web-services-with-angular-2305b8fd654b) blog post to learn more.
10
+
11
+In `src/app/app.module.ts`, I added `import { HttpClientModule } from '@angular/common/http';` to the import statement up top.
12
+Then in the `imports` section, I added `HttpClientModule`.
13
+
14
+In `src/pages/home/home.ts`, I did the following:
15
+1. added `import { ApiProvider } from './../../providers/api/api';` on the top of the file
16
+2. added `import { User } from './../../app/users/user';` on the top of the file
17
+3. injected in the `public apiProvider: ApiProvider` in the constructor method
18
+4. Added these two methods to tell the app to make the call to the server when it's initialized:
19
+
20
+```
21
+ngOnInit() {
22
+  this.loadUsers();
23
+}
24
+
25
+loadUsers() {
26
+  this.apiProvider.getUsers().subscribe(
27
+    data => this.users = data["_embedded"]["users"],
28
+    err => {
29
+        console.log(err);
30
+    });
31
+}
32
+```
33
+
34
+https://ionicacademy.com/http-calls-ionic/
35
+https://scotch.io/tutorials/angular-2-http-requests-with-observables

+ 5
- 0
package-lock.json Parādīt failu

@@ -104,6 +104,11 @@
104 104
       "resolved": "https://registry.npmjs.org/@ionic-native/core/-/core-4.9.1.tgz",
105 105
       "integrity": "sha512-5NLeMQ4C8VH0fApJw/G2U2AMqMCaiWxiky1k3Mr5ShYaw6QvSQrLxD06rInOg5po59n02qdcqCiSEw1JnQYn1g=="
106 106
     },
107
+    "@ionic-native/http": {
108
+      "version": "4.9.1",
109
+      "resolved": "https://registry.npmjs.org/@ionic-native/http/-/http-4.9.1.tgz",
110
+      "integrity": "sha512-gr0CCbixUDBal860iJDfI/TFG5K03bOOi9Xac3JZKvfpVWOHdttLHrauRVLnbyc1a18/CQXR3tLTu6UcsRw5LA=="
111
+    },
107 112
     "@ionic-native/splash-screen": {
108 113
       "version": "4.9.1",
109 114
       "resolved": "https://registry.npmjs.org/@ionic-native/splash-screen/-/splash-screen-4.9.1.tgz",

+ 1
- 0
package.json Parādīt failu

@@ -22,6 +22,7 @@
22 22
     "@angular/platform-browser": "5.2.11",
23 23
     "@angular/platform-browser-dynamic": "5.2.11",
24 24
     "@ionic-native/core": "4.9.1",
25
+    "@ionic-native/http": "^4.9.1",
25 26
     "@ionic-native/splash-screen": "4.9.1",
26 27
     "@ionic-native/status-bar": "4.9.1",
27 28
     "@ionic/pro": "1.0.20",

+ 7
- 2
src/app/app.module.ts Parādīt failu

@@ -4,8 +4,11 @@ import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular';
4 4
 import { SplashScreen } from '@ionic-native/splash-screen';
5 5
 import { StatusBar } from '@ionic-native/status-bar';
6 6
 
7
+import { HttpClientModule } from '@angular/common/http';
8
+
7 9
 import { MyApp } from './app.component';
8 10
 import { HomePage } from '../pages/home/home';
11
+import { ApiProvider } from '../providers/api/api';
9 12
 
10 13
 @NgModule({
11 14
   declarations: [
@@ -14,7 +17,8 @@ import { HomePage } from '../pages/home/home';
14 17
   ],
15 18
   imports: [
16 19
     BrowserModule,
17
-    IonicModule.forRoot(MyApp)
20
+    IonicModule.forRoot(MyApp),
21
+    HttpClientModule
18 22
   ],
19 23
   bootstrap: [IonicApp],
20 24
   entryComponents: [
@@ -24,7 +28,8 @@ import { HomePage } from '../pages/home/home';
24 28
   providers: [
25 29
     StatusBar,
26 30
     SplashScreen,
27
-    {provide: ErrorHandler, useClass: IonicErrorHandler}
31
+    {provide: ErrorHandler, useClass: IonicErrorHandler},
32
+    ApiProvider
28 33
   ]
29 34
 })
30 35
 export class AppModule {}

+ 6
- 0
src/app/users/user.ts Parādīt failu

@@ -0,0 +1,6 @@
1
+export class User {
2
+    constructor(
3
+        public id: number,
4
+        public name: string
5
+    ){}
6
+}

+ 5
- 4
src/pages/home/home.html Parādīt failu

@@ -7,8 +7,9 @@
7 7
 </ion-header>
8 8
 
9 9
 <ion-content padding>
10
-  The world is your oyster.
11
-  <p>
12
-    If you get lost, the <a href="http://ionicframework.com/docs/v2">docs</a> will be your guide.
13
-  </p>
10
+  <ul>
11
+    <li *ngFor="let user of users">
12
+      {{user.name}}
13
+    </li>
14
+  </ul>
14 15
 </ion-content>

+ 15
- 1
src/pages/home/home.ts Parādīt failu

@@ -1,5 +1,7 @@
1 1
 import { Component } from '@angular/core';
2 2
 import { NavController } from 'ionic-angular';
3
+import { ApiProvider } from './../../providers/api/api';
4
+import { User } from './../../app/users/user';
3 5
 
4 6
 @Component({
5 7
   selector: 'page-home',
@@ -7,8 +9,20 @@ import { NavController } from 'ionic-angular';
7 9
 })
8 10
 export class HomePage {
9 11
 
10
-  constructor(public navCtrl: NavController) {
12
+  users: User[];
11 13
 
14
+  constructor(public navCtrl: NavController, public apiProvider: ApiProvider) {
12 15
   }
13 16
 
17
+  ngOnInit() {
18
+    this.loadUsers();
19
+  }
20
+
21
+  loadUsers() {
22
+    this.apiProvider.getUsers().subscribe(
23
+      data => this.users = data["_embedded"]["users"],
24
+      err => {
25
+          console.log(err);
26
+      });
27
+  }
14 28
 }

+ 14
- 0
src/providers/api/api.ts Parādīt failu

@@ -0,0 +1,14 @@
1
+import { HttpClient } from '@angular/common/http';
2
+import { Injectable } from '@angular/core';
3
+
4
+@Injectable()
5
+export class ApiProvider {
6
+
7
+  constructor(public http: HttpClient) {
8
+  }
9
+
10
+  getUsers() {
11
+    return this.http.get('http://localhost:8080/users');
12
+  }
13
+
14
+}