2017-02-25 46 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

非常感謝! –