Angular http

一、引用

引用即使用前的准备,要使用http请求,根据mvc的模块设计,应该从model层入手,即在service中使用。

1.1 在app.module.ts中导入HttpClientModule模块

import { HttpClientModule } from '@angular/common/http';

 

二、使用

2.1 在service层中引入并使用

cart.services.ts:

/*
 * @Author: tom 
 * @Date: 2019-11-10 13:03:06 
 * @Last Modified by: tom
 * @Last Modified time: 2019-11-10 15:11:12
 */

import { Injectable } from '@angular/core';
import { products } from 'src/assets/products';
import { HttpClient } from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})

export class CartService {

  // 构建数组,保存加入购物车的product对象
  items = [];
  
  constructor(
    private http: HttpClient
  ) { }

  // 添加到购物车
  addToCart(product) {
    this.items.push(product);
  }

  // 清空购物车
  clearCart() {
    this.items = [];
    return this.items;
  }

  // 获取购物车内容,用于购物车组件界面显示
  getItem() {
    return this.items;
  }

  // 通过http请求获取到需要的数据,并返回给控制层
  getShippingByHTTP() {
    return this.http.get('../../assets/shipping.json');
  }

}

此处即为通过http的get请求方式获取到json数据

]

shipping.json:

[
    {
      "type": "Overnight",
      "price": 25.99
    },
    {
      "type": "2-Day",
      "price": 9.99
    },
    {
      "type": "Postal",
      "price": 2.99
    }
  ]

2.2 在购物车中使用

product-shipping.component.ts:

import { CartService } from './../../Service/cart.service';
import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-product-shipping',
  templateUrl: './product-shipping.component.html',
  styleUrls: ['./product-shipping.component.css']
})
export class ProductShippingComponent implements OnInit {

  shippingCosts;
  
  constructor(
    private cartService : CartService
  ) { 
    this.shippingCosts = this.cartService.getShippingByHTTP();
  }

  ngOnInit() {
  }

}

product-shipping.component.html:

<!-- /*
 * @Author: tom 
 * @Date: 2019-11-10 14:58:48 
 * @Last Modified by:   tom 
 * @Last Modified time: 2019-11-10 14:58:48 
 * @Description: this view is used to see the price of shipping.
                 the price data are comming from http request, 
                 so we should add async for get async data.
 */ -->

<p>
  product-shipping works!
</p>

<h3>Shipping Price</h3>

<!-- get async data from http request. and respose data is a array. -->
<div *ngFor="let item of shippingCosts | async">
  {{item.type}}
  {{item.price | currency}}
</div>

可以看到在for循环获取数据时,我还加了一层 async管道,async会将这一个模块包装进一个异步测试区域。当该区域中的所有异步调用都已完成时,这个dom结构会自动渲染。

得到最终结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Angular HTTPAngular框架中的一个模块,用于处理HTTP请求和响应。它提供了一组功能强大的API,可以方便地进行HTTP通信。在Angular中,可以使用HttpClient模块来发送HTTP请求,并使用Observables来处理响应。 要在Angular中使用HTTP模块,首先需要在应用的根模块或特定模块中导入HttpClientModule。例如,在app.module.ts文件中,可以使用以下代码导入HttpClientModule: import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { HttpClientModule } from '@angular/common/http'; @NgModule({ imports: [ BrowserModule, HttpClientModule, // ... 其他模块 ... ], declarations: [ AppComponent ], bootstrap: [ AppComponent ] }) export class AppModule {} 一旦导入了HttpClientModule,就可以在组件中注入HttpClient服务,并使用它来发送HTTP请求。例如,可以在组件中注入HttpClient,并使用get()方法发送一个简单的GET请求: import { HttpClient } from '@angular/common/http'; @Component({ // 省略组件的其他配置 }) export class MyComponent { constructor(private http: HttpClient) {} getData() { this.http.get('http://example.com/api/data').subscribe((response) => { // 处理响应数据 }); } } 以上示例代码演示了如何使用Angular HTTP模块发送一个简单的GET请求。根据实际需求,还可以使用HttpClient模块发送POST、PUT、DELETE等类型的请求,并使用不同的参数和选项来定制请求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值