加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Axios和Angular

发布时间:2020-12-17 17:43:23 所属栏目:安全 来源:网络整理
导读:试图找出如何在Angular cli,基础脚手架中进行axios导入和使用. 我没有找到任何文件. 我找到了这个 https://netbasal.com/angular-2-use-your-own-http-library-b45e51b3525e 他的建议并不适用. Cli在添加任何代码位时中断. 有关如何在Angular中进行基本的后a
试图找出如何在Angular cli,基础脚手架中进行axios导入和使用.

我没有找到任何文件.

我找到了这个
https://netbasal.com/angular-2-use-your-own-http-library-b45e51b3525e

他的建议并不适用. Cli在添加任何代码位时中断.

有关如何在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里面,但我已经有:

enter image description here

解决方法

最有可能使用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客户端更强大,所以我做不到想一想要求你进行这种转换的原因

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读