物业&#39;订阅&#39;在类型上不存在。甚至返回Observable <any>

时间:2018-05-08 14:21:41

标签: angular typescript rxjs

当我尝试订阅服务中的方法时,我收到了编译错误,是的,我正在返回一个Observable。

相关版本来自默认的.net核心角度模板。

  • Angular =&gt; 4.4.4
  • rxjs =&gt; 5.4.2

VS代码错误

VS Code error. 下面是带引用的login.component.ts。

login.component.ts

import { Component, OnInit } from "@angular/core";
import { LoginViewModel } from "../models/ApplicationUserViewModel";

import { LoginService } from "../services/Login.service";

@Component({
  selector: "app-login",
  templateUrl: "./login.component.html",
  styleUrls: ["./login.component.css"]
})
export class LoginComponent implements OnInit {
  public loginViewModel: LoginViewModel;

  constructor(private loginService: LoginService) {
    this.loginViewModel = new LoginViewModel();
  }

  ngOnInit() {}

  login(): any {
    this.loginService.login(this.loginViewModel).subscribe(
      result => {
        alert("success");
      },
      error => {
        alert("error");
      }
    );
  }
}

下面是带参考的login.service.ts。

login.service.ts

import { Injectable } from "@angular/core";
import { Observable } from "rxjs/Rx";
import { of } from "rxjs/observable/of";
import { HttpClient, HttpHeaders } from "@angular/common/http";
import { map } from "rxjs/operator/map";

import { LoginViewModel } from "../models/ApplicationUserViewModel";

const httpOptions = {
  headers: new HttpHeaders({ "Content-Type": "application/json" })
};

@Injectable()
export class LoginService {
  constructor(private http: HttpClient) {}

  login(loginViewModel: LoginViewModel): Observable<any> {
    return this.http.post<LoginViewModel>(
      'account/login',
      loginViewModel,
      httpOptions
    );
  }
}

谢谢。

0 个答案:

没有答案