试图找出如何在Angular cli,基础脚手架中进行axios导入和使用.
我没有找到任何文件.
我找到了这个
https://netbasal.com/angular-2-use-your-own-http-library-b45e51b3525e
有关如何在Angular中进行基本的后api调用的任何见解?
我得到的最好的是(有角度的原生方法):
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) }) } }
在app.component.ts里面,但我已经有:
解决方法
最有可能使用Axios的具体类而不是Angular的Http具体类不是一种有效的方法,因为每个类都有不同的接口.
你需要
> npm install @ types / axios
>在typescript中导入任何其他第三方库时导入axios
请注意,建议使用Angular的Http客户端模块
来自axios github页面:
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客户端比Angular JS的Http客户端更强大,所以我做不到想一想要求你进行这种转换的原因