angular入门基础教程(十一)与服务端数据交互
创始人
2024-11-15 19:10:02
0

前后端分离开发,少不了与后端进行数据接口的对接,在vue,react中我们要借助第三方的axios来进行数据请求。在ng中,为我们封装了了一层httpClient,我们直接使用即可

依赖注入

我们需要再次封装一次

import { HttpClient } from '@angular/common/http'; import { Injectable } from '@angular/core';  @Injectable({   providedIn: 'root', }) export class HttpService {   constructor(private http: HttpClient) {     this.http = http;   }    getData() {     return this.http.get('https://xxxxxxxxxx');   } } 

组件中使用

依赖注入,我们封装好了的接口方法

import { Component, inject } from '@angular/core'; import { HttpService } from '../../services/http.service'; import { firstValueFrom } from 'rxjs'; @Component({   selector: 'app-about',   standalone: true,   imports: [FormsModule, UpperCasePipe],   templateUrl: './about.component.html',   styleUrl: './about.component.css', }) export class AboutComponent {    dataLists: any = [];   //依赖注入,我们封装好了的接口方法   constructor(     private carServices: CarServiceService,     private httpService: HttpService   ) {   }    //声明周期   async ngOnInit() {     console.log('ngOnInit');      //将获取接口数据的.subscribe改造成了async await     const products = await firstValueFrom(this.httpService.getData());     console.log('🚀 ~ AboutComponent ~ ngOnInit ~ res:', products);     this.dataLists = products;   } } 

在这里插入图片描述

在这里插入图片描述
这样我们就获取到了后端数据

关于httpClient还有很多配置项与其他的几种请求方式,可以参考官网自行测试

相关内容

热门资讯

最新技巧!aapoker ai... 最新技巧!aapoker ai软件,aapoker猫腻,靠谱教程(先前存在有挂);小薇(透视辅助)致...
aapoker辅助工具存在!a... aapoker辅助工具存在!aapoker德州俱乐部,(aapoker苹果版)素来是真的有挂(详细辅...
wepoke辅助技巧(透视)w... wepoke辅助技巧(透视)wepooke系统规律(详细辅助AA德州教程)其实有挂(专业透视挂)1、...
重大来袭!微扑克中牌率,wep... 重大来袭!微扑克中牌率,wepoke计算辅助,辅助教程(好像是有挂)是一款可以让一直输的玩家,快速成...
aapoker辅助工具!aap... 您好,aapoker辅助工具这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多...
wepokeai代打(透视)w... wepokeai代打(透视)wepoke的机制(详细辅助高科技教程)原来是真的有挂(攻略是真的有挂)...
一分钟教你!德州之星app辅助... 一分钟教你!德州之星app辅助,德州之星外挂,介绍教程(切实是真的有挂);小薇(透视辅助)致您一封信...
aapoker猫腻!德州aap... 此外,数据分析德州(aapoker辅助工具)辅助神器app还具备辅助透视行为开挂功能,通过对客户aa...
wepoke黑科技(透视)we... wepoke黑科技(透视)wepoke辅助软件下载(详细辅助技巧教程)其实是有挂(可靠外 挂)wep...
技术分享!pokerworld... 1、技术分享!pokerworld有挂吗,德扑之星有作弊,安装教程(本然是真的有挂);代表性(透视辅...