调用服务 angular2 错误“属性"在“订阅"类型上不存在 [英] calling a service angular2 error 'Property 'then' does not exist on type 'Subscription'
本文介绍了调用服务 angular2 错误“属性"在“订阅"类型上不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从我的 login.ts 调用服务,但我不断收到不同的错误.这是我的代码
I am trying to call service from my login.ts but i keep getting different errors. here is my code
login.ts
import { Component } from '@angular/core';
import { Auth, User } from '@ionic/cloud-angular';
import { NavController } from 'ionic-angular';
import { Storage } from '@ionic/storage';
import { TabsPage } from '../tabs/tabs';
import { AuthService } from '../../services/auth/auth.service';
import {Observable} from 'rxjs/Rx';
@Component({
templateUrl: 'login.html'
})
export class LoginPage {
authType: string = "login";
error: string;
storage: Storage = new Storage();
constructor(public auth: Auth, public user: User, public navCtrl: NavController, public authService: AuthService) {}
facebookLogin() {
this.auth.login('facebook').then((success) => {
this.authService.signup(this.user.social.facebook).then((success) => {
});
});
}
}
auth.service.ts
auth.service.ts
import { Storage } from '@ionic/storage';
import { AuthHttp, JwtHelper, tokenNotExpired } from 'angular2-jwt';
import { Injectable, NgZone } from '@angular/core';
import { Observable } from 'rxjs/Rx';
import { Http, Headers } from 'angular2/http';
@Injectable()
export class AuthService {
jwtHelper: JwtHelper = new JwtHelper();
// contentHeader = new Headers({"Content-Type": "application/json"});
storage: Storage = new Storage();
refreshSubscription: any;
user: Object;
zoneImpl: NgZone;
idToken: string;
error: string;
constructor(private authHttp: AuthHttp, zone: NgZone) {
this.zoneImpl = zone;
// Check if there is a profile saved in local storage
this.storage.get('profile').then(profile => {
this.user = JSON.parse(profile);
}).catch(error => {
console.log(error);
});
this.storage.get('id_token').then(token => {
this.idToken = token;
});
}
public authenticated() {
return tokenNotExpired('id_token', this.idToken);
}
public signup(params) {
var url = 'http://127:0.0.1:3000/api/v1/register';
return this.authHttp.post(url, JSON.stringify(params))
.map(res => res.json())
.subscribe(
data => {this.storage.set('id_token', data.token)},
err => this.error = err
);
}
}
所以基本上我想要的是当点击 facebookLogin()
时,它会转到 auth.service.ts
并获取 singup()
返回值的方法.
SO basically what i want is when the facebookLogin()
is clicked, it goes to auth.service.ts
and gets the singup()
method which returns the value.
但我不断收到 Subscription"类型的属性then"不存在.
这是什么意思,我该如何解决?
What does this mean and how do i resolve this?
推荐答案
改订阅toPromise()
(别忘了导入toPromise
)
public signup(params) {
var url = 'http://127:0.0.1:3000/api/v1/register';
return this.authHttp.post(url, JSON.stringify(params))
.map(res => res.json())
.toPromise(
data => {this.storage.set('id_token', data.token)},
);
}
或 .map()
然后在调用站点上使用 subscribe()
而不是 then()
.
or to .map()
and then use subscribe()
instead of then()
on the call site.
public signup(params) {
var url = 'http://127:0.0.1:3000/api/v1/register';
return this.authHttp.post(url, JSON.stringify(params))
.map(res => {
let data = res.json();
this.storage.set('id_token', data.token);
return data;
});
}
facebookLogin() {
this.auth.login('facebook').then((success) => {
this.authService.signup(this.user.social.facebook).subscribe((success) => {
});
});
}
这篇关于调用服务 angular2 错误“属性"在“订阅"类型上不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文