angular - 类型上不存在属性“订阅”。甚至返回 Observable
问题描述
当我尝试订阅服务中的方法时收到编译错误,是的,我正在返回一个 Observable。
相关版本来自默认的 .net core angular 模板。
- 角度 => 4.4.4
- rxjs => 5.4.2
VS 代码错误
登录组件.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。
登录服务.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
);
}
}
谢谢你。
解决方案
推荐阅读
- python - 在每行的某个值内计算numpy数组中的实例
- ruby-on-rails - 未知提供者:e 将 Angular/Rails 应用程序移至生产环境时
- reactjs - 如何通过 fetch-interceptor 在 ReactJs 中设置凭据属性
- itext - 可填写的表格:两份填写相同的表格,第二份不打印数据
- swift - 使用 NSBatchUpdate 函数根据标准更新核心数据数据库
- gcc - 直接使用操作码而不是助记符的内联 AT&T asm 语法
- node.js - 使用 Dat 的双向数据流
- python - 按列值设置 Python 子集
- python - 将偶数行和奇数行分成两个新列
- python - 熊猫从上一行中减去另一列中的值