• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

angular-基础教程httpclient

武飞扬头像
大佬别喷记录博客只为自己学习记录
帮助1

官网:使用 HTTP 与后端服务进行通信

基础用法

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
系列文章
更多 icon
同类精品
更多 icon
继续加载