当我尝试订阅服务中的方法时,我收到了编译错误,是的,我正在返回一个Observable。
相关版本来自默认的.net核心角度模板。
VS代码错误
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
);
}
}
谢谢。