Axios 和 Angular [英] Axios and Angular
问题描述
试图弄清楚如何在 Angular cli、基础脚手架中导入和使用 axios.
我没有找到任何相关文档.
我发现了这个
很可能使用 Axios 的具体类而不是 Angular 的 Http 具体类并不是一种有效的方法,因为这些类中的每一个都有不同的接口.
你需要
- npm install @types/axios
- 在 typescript 中导入任何其他第三方库时导入 axios
注意推荐使用Angular的Http Client Module
来自 axios github 页面:
<块引用>axios 深受 Angular 提供的 $http 服务的启发.最终 axios 是努力提供一个独立的 $http-like在 Angular 之外使用的服务"
Angular 的 (Angular 2+) Http客户端比 Angular JS 的 Http 客户端更强大,所以我不能想出一个需要你进行这个转换的原因
Trying to figure out how to make an axios import and use in an Angular cli, base scaffolding.
I didn't find any docs for this.
I found this https://netbasal.com/angular-2-use-your-own-http-library-b45e51b3525e
what he suggests does not apply. the Cli breaks on adding any of his code bits.
Any insight on how to do a basic post api call in Angular?
best I got is (the angular native approach) :
import { Component } from '@angular/core';
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
@Injectable()
export class AppComponent{
title = 'prouuuut';
posts = [];
errors = [];
constructor(private http: Http){
}
fetchAll(){
this.http.get('https://api.github.com/repositories')
.then(response => {
this.posts = response.data.entry
})
.catch(e => {
this.errors.push(e)
})
}
}
inside app.component.ts but I already have :
Most probably using Axios's concrete class instead of Angular's Http concrete class is not a valid approach since each of these classes would have a different interface.
You need to
- npm install @types/axios
- import axios as you import any other third-party library in typescript
Note that it is recommended to use Angular's Http Client Module
From axios github page:
axios is heavily inspired by the $http service provided in Angular. Ultimately axios is an effort to provide a standalone $http-like service for use outside of Angular"
Angular's (Angular 2+) Http client is more powerful than Angular JS's Http client, so I could not think of a reason that requires you to be making this switch
这篇关于Axios 和 Angular的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!