2017-02-25 56 views
0

我正在尝试使用Angular 2构建MEAN应用程序。
流程就像是
login.component.ts交接事件调用auth.service.ts的getLogin功能
auth.service.ts作出承诺,以表达对网址/登录服务器。Angular2 Promise。然后未定义

但无法做出继续和得到这个错误

"EXCEPTION: Error in http://localhost:4000/app/components/account/login.component.html:4:0 caused by: this.authService.getLogin.then is not a function" core.umd.js:3064:13 

ORIGINAL EXCEPTION: this.authService.getLogin.then is not a function core.umd.js:3066:17 

Object { _view: Object, _nodeIndex: 10, _tplRow: 4, _tplCol: 0 } core.umd.js:3074:17 

Error: Error in http://localhost:4000/app/components/account/login.component.html:4:0 caused by: this.authService.getLogin.then is not a function zone.js:958:21 

login.component.ts

import {Component} from '@angular/core'; 
import {AuthService} from '../../services/auth.service'; 
@Component({ 
    moduleId: module.id, 
    selector: 'login', 
    templateUrl: './login.component.html' 
}) 
export class LogInComponent { 
     info:String=""; 
     username:String; 
     password:String; 
     constructor(private authService:AuthService){ 

     } 

     login(event){ 
      console.log("event call"); 
      event.preventDefault(); 
      var newAccount={ 
      username:this.username, 
      password:this.password 
      }; 
      this.authService.getLogin.then(data => this.info = dat); 
     } 

} 

auth.service.ts

import {Injectable} from '@angular/core'; 
import {Http, Headers} from '@angular/http'; 
import 'rxjs/add/operator/map'; 
@Injectable() 
export class AuthService { 
    constructor(private http:Http){ 
    } 
    getLogin(newAccount): Promise<any> { 
     return this.http.get("/log") 
       .toPromise() 
       .then(response => response.json().data) 
       .catch(this.handleError); 
    } 
} 

快速输出测试

router.get('/log', function(req, res) { 
     res.json({"status":"okay"}); 
}); 

经过快递服务器,它给正确的JSON输出

我做错了吗?

还有一件事是什么时候使用订阅vs地图vs然后。没有了解 的区别?(此链接可能有用)

尝试2天。如果需要发布任何代码。

+0

不应该服务仅返回刚刚答应..你在服务做着。于是以及组件 –

+0

其在呈三角其他工作案例 –

回答

0

login.component.ts您刚才调用了getLogin。它应该是功能 getLogin()for。然后按照auth.service.ts中的定义正常工作。

应该是这样

 this.authService.getLogin().then(data => this.info = dat); 

,而不是

 this.authService.getLogin.then(data => this.info = dat); 
+0

非常感谢! –