我使用 this tool根据我的模型生成了我的实体组件和服务. 一切正常,但我在尝试在API中记录用户时遇到了问题(功能正常). http.post()方法未被触发.我对角度很新,也无法弄清楚我做错了什么
一切正常,但我在尝试在API中记录用户时遇到了问题(功能正常).
http.post()方法未被触发.我对角度很新,也无法弄清楚我做错了什么.
任何帮助都会很棒!
这是我的UserService的登录方法(该方法被正确调用,它只是http.post()不起作用):
/** * Log a user by its credentials. */ login(username : string, password : string) : Observable<NmUser> { let body = JSON.stringify({ 'username': username, 'password': password }); console.log(body); return this.http.post(this.userUrl + 'login', body, httpOptions) .pipe( map(response => new NmUser(response)), catchError(this.handleError) ); } // sample method from angular doc private handleError (error: HttpErrorResponse) { // TODO: seems we cannot use messageService from here... let errMsg = (error.message) ? error.message : 'Server error'; console.error(errMsg); if (error.status === 401 ) { window.location.href = '/'; } return Observable.throw(errMsg); }
这是调用登录功能的页面:
import { Component } from '@angular/core'; import { NavController } from 'ionic-angular'; import { NmUserService } from '../../app/entities/nmUser/nmUser.service'; @Component({ selector: 'page-login', templateUrl: 'login.html', providers: [NmUserService] }) export class LoginPage { constructor(private navCtrl: NavController, private userService: NmUserService) { } login(username: string, password: string): void { this.userService.login(username, password); } }
谢谢 !
除非您订阅了observable,否则永远不会进行HTTP调用,这在组件中完成,如下所示:this.userService.login(username, password).subscribe((result) => { // This code will be executed when the HTTP call returns successfully });