1
- import { HttpClient } from '@angular/common/http' ;
1
+ import { HttpClient , HttpParams } from '@angular/common/http' ;
2
2
import { Component , ElementRef , ViewChild } from '@angular/core' ;
3
3
import { NgForm , NonNullableFormBuilder , Validators } from '@angular/forms' ;
4
- import { Observable , catchError , map , } from 'rxjs' ;
4
+ import { Observable , catchError , map , of , filter } from 'rxjs' ;
5
5
import { NzNotificationService } from 'ng-zorro-antd/notification' ;
6
6
import { UrlModel } from './models/demo.model' ;
7
7
import { Params } from '../../utils/params.service' ;
8
8
import { APIForm } from '@/types/demo' ;
9
+ import { DemosService } from './demos.service'
9
10
10
11
@Component ( {
11
12
selector : 'app-demos' ,
@@ -41,9 +42,10 @@ export class DemosComponent {
41
42
42
43
constructor (
43
44
private http : HttpClient ,
45
+ private service : DemosService ,
44
46
private params : Params ,
45
47
private notification : NzNotificationService ,
46
- private fb : NonNullableFormBuilder
48
+ private fb : NonNullableFormBuilder ,
47
49
) {
48
50
this . apiForm = this . fb . group < APIForm > ( {
49
51
email : '' ,
@@ -63,12 +65,18 @@ export class DemosComponent {
63
65
this . notification . warning ( '警告' , '请输入正确格式的状态码' ) ;
64
66
return ;
65
67
}
66
- console . log ( "this.status:" , this . status )
67
- // this.http.post('/test/status/' + this.status, {}).subscribe((res) => {
68
- // console.log(res);
69
- // }, (err) => {
70
- // console.log(err);
71
- // });
68
+ this . service . testStatus ( new HttpParams ( ) . set ( 'status' , this . status ) ) . subscribe ( {
69
+ next ( res ) {
70
+ console . log ( res ) ;
71
+ } ,
72
+ // error: console.error,
73
+ error ( err ) {
74
+ console . log ( err ) ;
75
+ } ,
76
+ complete ( ) {
77
+ console . log ( 'compelete...' )
78
+ }
79
+ } ) ;
72
80
}
73
81
74
82
public queryIPGeo ( ) {
@@ -134,14 +142,25 @@ export class DemosComponent {
134
142
}
135
143
136
144
apiSubmit ( ) {
145
+ // const observable$ = of(1, 2, 3);
146
+ // const modifiedObservable$ = observable$.pipe(
147
+ // map(value => value * 2),
148
+ // filter(value => value > 1)
149
+ // );
150
+ // modifiedObservable$.subscribe(console.log);
137
151
if ( this . apiForm . valid ) {
138
152
console . log ( 'submit' , this . apiForm . value ) ;
139
153
const { url, method, params } = this . apiForm . value
140
- const data = this . http . get ( url ) . pipe ( map ( res => {
141
- console . log ( res , '...' )
142
- return res
143
- } ) )
144
- console . log ( "data:" , data )
154
+ this . service . testApi ( url , params ) . subscribe ( {
155
+ next : ( res ) => {
156
+ this . apiForm . controls . res . setValue ( JSON . stringify ( res , null , 4 ) )
157
+ } ,
158
+ error : ( res ) => {
159
+ this . notification . error ( 'Error' , res . toString ( ) )
160
+ } ,
161
+ complete : ( ) => {
162
+ }
163
+ } )
145
164
} else {
146
165
Object . values ( this . apiForm . controls ) . forEach ( control => {
147
166
if ( control . invalid ) {
0 commit comments