angular使用http实现get和post请求

说明:

angular使用http实现get和post请求

提示:在运行本项目前,请先导入路由router,可以参考我上一篇文章。

效果图:

step1:E:\projectgood\ajsix\untitled4\package.json

"@angular/cdk": "^18.2.10",

"@angular/material": "^18.2.10",

step2:E:\projectgood\ajsix\untitled4\src\app\model\Service.ts

javascript 复制代码
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';

@Injectable()
export class Service {
  url = 'https://demo.restheart.org/messages';

  constructor(private http: HttpClient) {
  }

  public get(page: number = 1): Observable<Array<any>> {
    return this.http.get<Array<any>>(`${this.url}?pagesize=5&page=${page}`);
  }

  public size(): Observable<any> {
    return this.http.get(`${this.url}/_size`);
  }

  public delete(id: string): Observable<any> {
    return this.http.delete(`${this.url}/${id}`);
  }

  public getSingleCharacterz(id:string):Observable<any>{
    return this.http.get(`http://localhost:5000/api/water`)
  }

  public postSingleCharacterz(data: any): Observable<any> {
    const water =  {
      watername: '韩红牌纯净水',
      watericon: 'http://e.hiphotos.baidu.com/image/pic/item/a1ec08fa513d2697e542494057fbb2fb4316d81e.jpg',
      waterstyle: '最中幻想',
      watersize: '50L',
      waterprice: '32',
      shopaccount: '772024102801',
      shopname: '大润发超市'
    }
    return this.http.post(`http://localhost:5000/api/wateradd`, water);
  }


  public post(data: any): Observable<any> {
    const _data = {
      message: data.message,
      from: data.from
    };

    return this.http.post(this.url, _data);
  }
}

step3:E:\projectgood\ajsix\untitled4\src\app\user\user.component.html

javascript 复制代码
<p>user works!</p>
<button mat-flat-button color="primary" (click)="onAcceptClick('element.operate')">查询</button>
<button mat-flat-button color="primary" (click)="onAddClick('element.operate')">新增</button>

step4:E:\projectgood\ajsix\untitled4\src\app\user\user.component.ts

javascript 复制代码
import { Component, OnInit } from '@angular/core';
import { Observable, tap } from 'rxjs';
// import { Service } from './service';
import { FormsModule } from '@angular/forms';
import { AsyncPipe, JsonPipe, DatePipe } from '@angular/common';
import { HttpClient, HttpClientModule, HttpHandler } from '@angular/common/http';

import {Service} from '../model/Service';
import {MatButtonModule} from '@angular/material/button';

@Component({
  selector: 'app-user',
  standalone: true,
  imports: [FormsModule,AsyncPipe,JsonPipe,DatePipe,HttpClientModule,MatButtonModule],
  templateUrl: './user.component.html',
  providers:[Service],
  styleUrl: './user.component.css'
})
export class UserComponent implements OnInit  {

  constructor(private service: Service) {}


  ngOnInit(): void {
  }
  onAcceptClick(key: string): void {
    console.log('you click onAcceptClick'+key)


    this.service.getSingleCharacterz('1').subscribe(
      (result:any)=>{
        console.log(result)
      },(error:any)=>{
        console.error('Error : '+error);
      }
    );
  }


  onAddClick(key: string): void {
    console.log('you click onAddClick' + key)
    this.service
      .postSingleCharacterz('1')
      .pipe(tap())
      .subscribe((resp) =>{
        console.log(resp)
      });
  }

}

end

相关推荐
To_OC16 分钟前
从一个颜色选择器说起:我终于整明白了 React+TS 里的 model 与 api 分层
前端·react.js·typescript
赵大仁17 分钟前
浏览器端 RAG:Transformers.js + WebGPU 本地检索入门
前端·ai·react·webgpu·rag
木叶丸33 分钟前
从 Loop 到 Graph:AI 智能体协作系统工程指南
前端·后端·架构
吴懿不在 不负信仰2 小时前
从jQuery谈库与框架的设计之优劣
前端·javascript·jquery
灵析表格2 小时前
灵析表格手机号处理函数深度分析报告
前端·网络·json·wps·灵析表格·excel公式盒子
智海深蓝2 小时前
智慧渔业海上养殖数字孪生实践方向与难点拆解分析
java·前端·网络
丁引2 小时前
《数据清洗的艺术:如何用20行核心逻辑优雅地删除无标签图片》
前端·数据库·python
Bigger3 小时前
Han:一个让 AI Agent 也能做出高级中国风页面的 CSS 设计系统
前端·ai编程·设计
梦想的旅途23 小时前
企业微信自动化:自动发送文本、图片、文件
前端·数据库·microsoft
MartinYeung54 小时前
npm爆发大规模供应链攻击 蠕虫污染 2000+ 个包版本: 深度技术剖析
前端·npm·node.js