角到.net核心Web API。获取rxjs_Observable__WEBPACK_IMPORTED_MODULE_2 __。Observable.throw不是函数

时间:2018-10-03 15:11:46

标签: angular asp.net-web-api .net-core

我正在尝试从托管在本地IIS上的.net核心Web API获取记录列表。我按照教程上的方法进行操作。我使它工作并显示了以下代码,该代码调用json测试网站。

  getUsers() {
    return this.http.get('http://jsonplaceholder.typicode.com/users')
      .map(response => response.json())
      .catch(this.handleError);
  }

下一步,我要将URL切换到本地。

 getUsers() {
    return this.http.get('http://localhost:8081/api/account')
      .map(response => response.json())
      .catch(this.handleError);
  }

我不确定为什么角度页面会显示来自json测试站点而不是我的本地iis的内容。当我测试webapi时,它可以与邮递员和Chrome网址一起使用。

我还添加了导入'rxjs / add / Observable / throw',但是抛出了相同的错误。

为什么它可以与测试站点的json提要一起使用,但不能与我的网络api一起使用。那是我的困惑,以及解决方法。

非常感谢您的帮助。

我击中f12时,看到以下错误

  

core.js:1673错误TypeError:   rxjs_Observable__WEBPACK_IMPORTED_MODULE_2 __。Observable.throw不是   功能       在CatchSubscriber.push ../ src / app / services / user.service.ts.UserService.handleError   [作为选择器](user.service.ts:38)       在CatchSubscriber.push ../ node_modules / rxjs / _esm5 / internal / operators / catchError.js.CatchSubscriber.error中   (catchError.js:34)       在MapSubscriber.push ../ node_modules / rxjs / _esm5 / internal / Subscriber.js.Subscriber._error中   (Subscriber.js:83)       在MapSubscriber.push ../ node_modules / rxjs / _esm5 / internal / Subscriber.js.Subscriber.error中   (Subscriber.js:61)       在XMLHttpRequest.onError(http.js:1066)       在ZoneDelegate.push ../ node_modules / zone.js / dist / zone.js.ZoneDelegate.invokeTask   (zone.js:421)       在Object.onInvokeTask(core.js:3811)       在ZoneDelegate.push ../ node_modules / zone.js / dist / zone.js.ZoneDelegate.invokeTask   (zone.js:420)       在Zone.push ../ node_modules / zone.js / dist / zone.js.Zone.runTask(zone.js:188)       在ZoneTask.push ../ node_modules / zone.js / dist / zone.js.ZoneTask.invokeTask中   [作为调用](zone.js:496)

user.service.ts代码

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

import 'rxjs/Rx';
import { Observable } from 'rxjs/Observable';
import { Http, Response } from '@angular/http';
import 'rxjs/add/operator/map';
import { User } from '../models/user';
import { throwError } from 'rxjs';

@Injectable({
  providedIn: 'root'
})

export class UserService {
  constructor(private http: Http) { }

  getUsers() {
    return this.http.get('http://localhost:8081/api/account')
      .map(response => response.json())
      .catch(this.handleError);
  }
  private handleError(error: Response) {
    console.error(error);
    return Observable.throw(error.json() || 'Server error');
  }
}

这是我的用户组件代码:

import { Component, OnInit } from '@angular/core';

import { UserService } from '../../services/user.service';
import { Observable } from 'rxjs';
import { User } from '../../models/user';
import 'rxjs/add/Observable/throw'

@Component({
  selector: 'app-users',
  templateUrl: './users.component.html',
  styleUrls: ['./users.component.scss']
})

export class UsersComponent implements OnInit {

  errorMessage: string;
  users$; Object;

  constructor(private userService: UserService) { }

  ngOnInit() {
    this.getUsers();
  }

  getUsers() {
    // need to subscriber to the Observable method to get data
     this.users$ = this.userService.getUsers()
         .subscribe(( userData )=> this.users$ = userData );
  }

}

0 个答案:

没有答案