Angular2 Promise .then 未定义
Angular2 Promise .then is not defined
我正在尝试使用 Angular 2 构建 MEAN 应用程序。
流量就像
login.component.ts 处理调用 auth.service.ts
的 getLogin 函数的事件
auth.service.ts 在 url /login.
上对 Express 服务器做出承诺
但无法继续并出现此错误
"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 输出
我做错了什么?
还有一件事是什么时候使用 subscribe vs map vs then。没看懂
区别?(为此 link 可能会有用)
尝试从 2 days.Will post 如果需要任何代码。
在 login.component.ts 中,您刚刚调用了 getLogin。应该是函数
.then 的 getLogin() 按照 auth.service.ts.
中的定义正常工作
应该是
this.authService.getLogin().then(data => this.info = dat);
而不是
this.authService.getLogin.then(data => this.info = dat);
我正在尝试使用 Angular 2 构建 MEAN 应用程序。
流量就像
login.component.ts 处理调用 auth.service.ts
的 getLogin 函数的事件
auth.service.ts 在 url /login.
但无法继续并出现此错误
"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 输出
我做错了什么?
还有一件事是什么时候使用 subscribe vs map vs then。没看懂
区别?(为此 link 可能会有用)
尝试从 2 days.Will post 如果需要任何代码。
在 login.component.ts 中,您刚刚调用了 getLogin。应该是函数 .then 的 getLogin() 按照 auth.service.ts.
中的定义正常工作应该是
this.authService.getLogin().then(data => this.info = dat);
而不是
this.authService.getLogin.then(data => this.info = dat);