angular-基础教程httpclient
基础用法
app.module.ts
import { HttpClientModule } from '@angular/common/http'
@NgModule({
imports: [
HttpClientModule
]
})
export class AppModule { }
app.component.ts
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {
title = 'angular-base';
constructor(private http: HttpClient) {}
loadData() {
this.http.get(url).subscribe((res: any) => {
console.log(res)
})
}
}
封装到service服务中
全局配置src/app/config.ts
export const baseUrl = "http://localhost:3000"
接口
demo.type.ts
export interface Todo {
id: number,
name: string,
done: boolean
}
创建一个服务
一般在服务中返回数据,在组件中subscribe()service-demo.service.ts
import { Injectable } from '@angular/core';
// 导入HttpClient
import { HttpClient } from '@angular/common/http'
// 导入配置
import { baseUrl } from '../config'
import { Todo } from './demo.type';
@Injectable({
providedIn: 'root'
})
export class ServiceDemoService {
constructor(private http: HttpClient) { }
serviceMethod() {
return "这是service中的方法"
}
loadData() {
const url = `${baseUrl}/todos`
return this.http.get<Todo[]>(url, {
// 获取完整响应体
// observe: 'response'
// headers: {
// Authorization: `Bearer ${token}`
// }
})
}
}
在组件中调用http-demo.component.ts
import { Component, OnInit } from '@angular/core';
import { ServiceDemoService } from '../service-demo.service';
import { Todo } from '../demo.type';
@Component({
selector: 'app-http-demo',
templateUrl: './http-demo.component.html',
styleUrls: ['./http-demo.component.scss']
})
export class HttpDemoComponent implements OnInit {
constructor(private sdService: ServiceDemoService) { }
ngOnInit(): void {
}
todoList: Todo[] = []
loadData() {
this.sdService.loadData().subscribe((res: Todo[]) => {
console.log(res)
// this.todoList = res
// 优化:拷贝后赋值
this.todos = [...todos]
}, err => {
console.log(err)
})
}
}
GET / POST / DELETE / PUT
通用方法
var option = { body: user };
this.http.request("post", "http://localhost/api/v1/sys/login", option).subscribe(
(res: any) => {
console.log(res)
if (res.code == '200') {
this.isLogin = true
}
x.next(res);
x.complete();
},
(y) => {
x.error(y);
x.complete();
},
() => {
x.complete();
}
)
POST
this.http.post("http://localhost/api/v1/sys/login", {
"username": user.username,
"password": user.password
}).subscribe(
(res: any) => {
console.log(res)
if (res.code == '200') {
this.isLogin = true
}
x.next(res);
x.complete();
},
(y) => {
x.error(y);
x.complete();
},
() => {
x.complete();
}
)
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhgafeeh
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01 -
怎样阻止微信小程序自动打开
PHP中文网 06-13