GVKun编程网logo

Angular JS在调用新的$http之前取消$http调用(angularjs调用接口)

16

如果您想了解AngularJS在调用新的$http之前取消$http调用和angularjs调用接口的知识,那么本篇文章将是您的不二之选。我们将深入剖析AngularJS在调用新的$http之前取消$

如果您想了解Angular JS在调用新的$http之前取消$http调用angularjs调用接口的知识,那么本篇文章将是您的不二之选。我们将深入剖析Angular JS在调用新的$http之前取消$http调用的各个方面,并为您解答angularjs调用接口的疑在这篇文章中,我们将为您介绍Angular JS在调用新的$http之前取消$http调用的相关知识,同时也会详细的解释angularjs调用接口的运用方法,并给出实际的案例分析,希望能帮助到您!

本文目录一览:

Angular JS在调用新的$http之前取消$http调用(angularjs调用接口)

Angular JS在调用新的$http之前取消$http调用(angularjs调用接口)

在角度JS 1.1.5中,您可以取消之前启动的$http调用.这两个 link1和 link2对它的工作原理给出了一些解释.

但是我无法理解,如何在实践中使用它.我创建了plnkr以说明我想要实现的目标.

>用户有一个他可以点击的链接(我们知道,用户倾向于非常热情地这样做)
>如果用户单击链接,并且之前的请求尚未完成,则应取消它,并且应该发出新请求(在此示例中,还有其他方法可以忽略多次点击.原始问题是用户输入字段,其中包含ng-change on它 – 每次用户更改值时都会发出请求 – 每个按键都会触发$http.我正在使用链接使其更简单一些)

正如您在plnkr中看到的请求触发并立即取消,但我期待最后一个请求成功.

我是angularjs的新手,所以我可能做错了.我已经完成了我能想象到的一切,但是没有完整的例子来表示canceler.resolve()

任何人都可以帮我这个或指导我正确的方向吗?

解决方法

您需要为每个请求创建一个新的取消器,因为一旦解决它就无法再次使用.

See here for an updated plnkr.

Angular 2将三个http调用与flatMap相结合? RxJs?

Angular 2将三个http调用与flatMap相结合? RxJs?

我可以找到很多链接两个呼叫的例子,但我有3个http呼叫使用前一个呼叫的数据一个接一个地进行.

我有两个使用flatMap工作

所以:

call1(params)
  .flatMap((res1)=> {
     return call2(params)
        .subscribe(r=>r...)

但对于三个电话我正在尝试同样的事情,但我认为你不能将flatMaps连在一起?

call1(params)
  .flatMap((res1)=> {
     return call2(params)
        .flatMap((res2)=> {
            return call3(params)
               .subscribe(r=>r...)

我收到一个错误,说订阅不能分配给类型观察输入.每个call1都从http操作返回一个observable.

谁能指出我正确的方向?

真的很感激它,因为它让我疯了!

谢谢
保罗

解决方法

您可以根据需要多次使用flatMap.但是,每次都必须返回一个Observable.
例如.

myFunc() // returns an Observable of type X
        .flatMap((res: X) => {
           // returns an Observable of type Y
        })
        .flatMap((res: Y) => {
           // returns an Observable of type Z
        })
        .flatMap((res: Z) => {
           // returns an Observable of type Q
        })
        .subscribe((res: Q) => {
           // some logic
        });

RxJs发生了变化

从RxJs v5.5开始,出现了Pipeable操作符.我们不再将一些运算符原型化为Observables,而是导入它们并按如下方式使用它们:

import { flatMap } from 'rxjs/operators';

myFunc() // returns an Observable of type X
    .pipe(
        flatMap((res: X) => {
           // returns an Observable of type Y
        }),flatMap((res: Y) => {
           // returns an Observable of type Z
        }),flatMap((res: Z) => {
           // returns an Observable of type Q
        })
    ).subscribe((res: Q) => {
       // some logic
    });

Angular 4.3 HttpClient (Angular访问 REST Web 服务) 一、Http 请求示例(Get)

Angular 4.3 HttpClient (Angular访问 REST Web 服务) 一、Http 请求示例(Get)

链接
开发工具:Visual Studio Code

在Angular 4.3中引入了新的HttpClientModule。 这个新模块可以在@ angular / common / Http包中找到,并且可以完全重新实现前一个HttpModule。新的HttpClient服务包含在HttpClientModule中,可用于启动HTTP请求并处理应用程序中的响应。

安装 4.3 项目

开始Angular 4.3项目第一步。最简单的是用Angular CLI (Angular command line interface).

  1. 如果系统还没有安装过Angular CLI ,先要执行下面的命令安装最新版(-g:全局安装):
    npm install -g @angular/cli@latest

    如果有安装cnpm,可用cnpm,下载速度更快
    cnpm install -g @angular/cli@latest
  2. 新建一个文件夹ng_http,用来存放项目。命令行切换到此目录,然后用VS Code打开。
    cd D:\Angular\NG_App
    code .
  3. 接着,就可以使用 ng命令了开始一个新的Angular 项目了:
    ng new nghttp01
    如果报错,并在错误日志记录总显示
    integrity checksum Failed when using sha512
    npm4升级npm5,在运行 npm install 时,可能会包这样的错误,执行
    npm cache clean --force
    删除项目文件夹,重新执行。
    ng new nghttp01
    执行成功后会产生 nghttp01文件夹与项目模板,依赖性也会自动安装。
  4. 检查并确认 package.json 中Angular相关所有依赖版本 号 >4.3.0。
  5. package.json 文件做过更新后,命令行中切换到nghttp01下,执行下面命令更新
    npm install

在项目中启用 HttpClient

1。为了在项目components 中使用 HttpClient 服务,第一步就是要在Angular应用中把 HttpClientModule 包含进来。首先在应用根module中导入 HttpClient module,对应文件app.module.ts。

import { browserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HttpClientModule } from '@angular/common/http';//新增行
import { AppComponent } from './app.component';
@NgModule({
  declarations: [
    AppComponent
  ],imports: [
    browserModule,HttpClientModule //新增行
  ],providers: [],bootstrap: [AppComponent]
})

2。上一步在根 Module 导入了HttpClientModule ,就可以在components中使用HttpClient了。使用HttpClient第一步是导入(import),还必须要注入(inject )到component class的构造函数中。如下所示:

import { Component } from '@angular/core';
import { HttpClient } from '@angular/common/http';//新增行
@Component({
  selector: 'app-root',templateUrl: './app.component.html',styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'app';
  constructor(private http: HttpClient){//新增行,httpclient注入到构造函数中,以使HttpClient可用。
  }
}

3。HttpClient使用XMLHttpRequest浏览器API来执行HTTP请求。 为了执行特定类型的HTTP请求,可以使用以下与HTTP动词相对应的方法:
**get
post
put
delete
patch
head
jsonP**

使用HttpClient 请求数据

1。 简单示例:使用 GitHub’s REST API 请求用户数据。修改app.component.ts代码(完整):

import { Component,OnInit } from '@angular/core';//新增导入OnInit
import { HttpClient } from '@angular/common/http';//新增行
@Component({
  selector: 'app-root',styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {//新增implements OnInit
  title = 'app';
  results = '';
  constructor(private http: HttpClient){///新增行,httpclient注入到构造函数中,以使HttpClient可用。
  }
  //新增ngOnInit()方法
  ngOnInit(): void {    this.http.get('https://api.github.com/users/seeschweiler').subscribe(data => {
      console.log(data);
    });
  }
}

2。项目启动:VS Code命令行执行。网页访问http://localhost:4200/
ng serve


Angular Universal FULL SSR +路由器不执行Http调用

Angular Universal FULL SSR +路由器不执行Http调用

如何解决Angular Universal FULL SSR +路由器不执行Http调用?

我实际上正在尝试NG Universal,但是从以下位置运行示例代码时:https://angular.io/guide/universal

https://angular.io/generated/zips/universal/universal.zip

我尝试检查一个完整的SSR应用程序是如何...每次路由器单击路由器链接时,路由器调用一个新的URL时,该应用程序似乎都没有向快递服务器发送请求以获取新的html。

它如何工作?有没有办法通过Angular Universal获得完整的SSR?

假设当我从:/ dashboard转到/ heroes到/ heroes /:id时,路由器每次从一个点移动到另一个点时,我都希望服务器执行所有的api调用,等等...渲染页面,然后发送渲染页面的HTML。

为什么?

为避免浏览器中的应用将任何api请求直接发送到我们的API。

要能够在服务器上登录用户的操作,而不能仅登录首页加载

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

angular – “类型’对象’不能分配给”使用新的HttpClient / HttpGetModule“

angular – “类型’对象’不能分配给”使用新的HttpClient / HttpGetModule“

在 Google’s official Angular 4.3.2 doc here之后,我能够从本地json文件中执行一个简单的get请求.我想练习从JSON占位符站点点击一个真正的端点,但是我在查找放在.subscribe()运算符中的内容时遇到了麻烦.我创建了一个IUser接口来捕获有效负载的字段,但是带有.subscribe(data => {this.users = data})的行会抛出错误类型’Object’不能分配给’IUser []’类型.处理这个问题的正确方法是什么?看起来非常基本,但我是一个菜鸟.

我的代码如下:

import { Component,OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { IUsers } from './users';

@Component({
  selector: 'pm-http',templateUrl: './http.component.html',styleUrls: ['./http.component.css']
})
export class HttpComponent implements OnInit {
  productUrl = 'https://jsonplaceholder.typicode.com/users';
  users: IUsers[];
  constructor(private _http: HttpClient) { }

  ngOnInit(): void {    
    this._http.get(this.productUrl).subscribe(data => {this.users = data});
  }

}
实际上你在这里有一些选项,但是使用泛型将它转换为你期望的类型.
http.get<IUsers[]>(this.productUrl).subscribe(data => ...

   // or in the subscribe
   .subscribe((data: IUsers[]) => ...

此外,我建议在您的模板中使用自动订阅/取消订阅的异步管道,特别是如果您不需要任何奇特的逻辑,并且您只是映射该值.

users: Observable<IUsers[]>; // different type Now

this.users = this.http.get<IUsers[]>(this.productUrl);

// template:
*ngFor="let user of users | async"

关于Angular JS在调用新的$http之前取消$http调用angularjs调用接口的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于Angular 2将三个http调用与flatMap相结合? RxJs?、Angular 4.3 HttpClient (Angular访问 REST Web 服务) 一、Http 请求示例(Get)、Angular Universal FULL SSR +路由器不执行Http调用、angular – “类型’对象’不能分配给”使用新的HttpClient / HttpGetModule“等相关内容,可以在本站寻找。

本文标签: