【angular-基础教程】httpclient

官网:使用 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();
  }
)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Angular中使用angular-gridster库,可以通过使用其提供的事件和方法来获取拖拽后的数据。 首先,我们需要在HTML文件中设置Gridster组件,并声明一个用于展示数据的数组: ```html <gridster [options]="gridsterOptions" (gridsterItemChange)="onItemChange($event)"> <gridster-item *ngFor="let item of gridsterItems" [item]="item"> <!-- content --> </gridster-item> </gridster> ``` 在组件的.ts文件中,需要定义gridsterOptions和gridsterItems变量,并使用相关的事件和方法来获取拖拽后的数据: ```typescript import { Component } from '@angular/core'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { gridsterOptions = { // gridster options }; gridsterItems = [ // initial items ]; onItemChange(event: any) { // get gridster item change event console.log(event); // here you can access the dragged item's updated data } } ``` 在onItemChange方法中,可以通过event参数访问拖拽后的数据。它包含了当前拖拽的GridsterItemComponent实例,我们可以从中获取更新后的数据。 例如,可以通过event.item获取更新后的位置信息、尺寸信息等。如果有其他自定义的数据需要获取,可以在GridsterItemComponent中设置相关属性并在event.item中访问。 以上是使用angular-gridster获取拖拽后数据的基本方法,你可以根据自己的需求进行进一步定制和处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值