Angular 6 rxjs管道不返回数据

时间:2018-06-14 12:12:16

标签: angular rxjs

使用最新版本的rxjs和Angular 6时遇到问题。 代码;

import { Observable, throwError } from "rxjs";
import { map, filter, catchError, mergeMap } from 'rxjs/operators';

isValidUser(user, password) : Observable<any> {

let urlString = "http://localhost....=" + user + "&password=" + password;

console.log(urlString)

return this._http.get(urlString, {withCredentials: true, responseType: 'json'})
.pipe(
  map((val: any[]) => {
    console.log("val",val);
    if (val.hasOwnProperty("GoldUser")) {
      if (val["GoldUser"][0]["_Qy"] == "GoldSecurityUser")
        this.isUserLoggedIn = true;
        return true;
      }
      return false;
  }), catchError( error => {
    console.log("error",error );
    return throwError( 'Something went wrong!' )
  })

);

}

问题是即使URL有效并且简单的浏览器休息调用返回json数据,代码也不返回任何内容。它永远不会命中我的console.log val语句。在Chrome中,开发人员工具“网络”标签也显示无活动。这是我第一次使用Angular6和rxjs V6管道,我对此感到有点难过。

非常感谢任何帮助。

ng --version

Angular CLI: 6.0.8
Node: 8.9.4
OS: win32 x64
Angular: 6.0.4
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.6.8
@angular-devkit/build-angular     0.6.8
@angular-devkit/build-optimizer   0.6.8 
@angular-devkit/core              0.6.8
@angular-devkit/schematics        0.6.8
@angular/cli                      6.0.8
@ngtools/webpack                  6.0.8
@schematics/angular               0.6.8
@schematics/update                0.6.8
rxjs                              6.2.1
typescript                        2.7.2
webpack                           4.8.3

顺便说一下,我看过rxjs的移植页面,但仍然无法看到我做错了...

谢谢,

标记。

1 个答案:

答案 0 :(得分:3)

您需要在observable上调用subscribe来执行请求。

https://angular.io/guide/observables#subscribing