我遇到了 Rxjs订阅的问题,它被调用了两次,我已经在google和StackOverflow中搜索了,已经有一些问题已经被问到但是没有一个问题对我的情况有帮助,我也没有发现我的代码有什么问题。
这是我的代码: 的 login.ts
import {Component} from '@angular/core';
import {
AlertController,
IonicPage,
LoadingController,
MenuController,
NavController,
NavParams,
ToastController
} from 'ionic-angular';
import { Storage } from '@ionic/storage';
import {AuthServiceProvider} from "../../providers/auth-service/auth-service";
import {DashboardPage} from "../dashboard/dashboard";
import {SignupPage} from "../signup/signup";
import {FormControl, FormGroup, Validators} from "@angular/forms";
import {Network} from "@ionic-native/network";
@IonicPage()
@Component({
selector: 'page-login',
templateUrl: 'login.html',
})
export class LoginPage{
loginForm: FormGroup;
user:any = new Object();
constructor(
public navCtrl: NavController,
public navParams: NavParams,
private authService:AuthServiceProvider,
private alertCtrl: AlertController,
public storage: Storage,
public menuCtrl: MenuController,
private loading: LoadingController,
private network: Network,
private toast: ToastController
) {
this.menuCtrl.enable(false,'menu');
this.loginForm= new FormGroup({
mobile: new FormControl('', [Validators.required]),
password: new FormControl('', [Validators.required]),
});
}
ionViewDidLoad() {
console.log("called")
}
login() {
if(this.network.type === 'none' || this.network.type === 'unknown'){
this.toast.create({
message:`You are offline, please connect your wifi or 3g`,
position:'top',
duration:3000
}).present();
} else {
let spinner = this.loading.create({
content: 'Please wait...'
});
spinner.present();
this.authService.userLogin(this.user)
.subscribe(
response => {
console.log(response);
if (response.status === "ok") {
console.log(response.status);
this.authService.isLoggedIn(response);
this.navCtrl.setRoot(DashboardPage);
} else {
console.log(response.status);
let alert = this.alertCtrl.create({
title: response.status,
subTitle: response.msg,
buttons: ['Dismiss']
});
alert.present()
}
spinner.dismiss();
})
}
}
}
当身份验证失败时,它会显示两次失败警报,并且当身份验证成功时,它也会显示失败警报,然后立即成功并转到仪表板页面。
这是我的auth-service.ts
userLogin(user):Observable<any>{
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
};
let ttl = 60*60*24*30;
let delay = 'all';
const url = ApiEndPoint+'customer_login';
let req = this.http
.post(url, user,httpOptions)
.map(data=> {
return data;
})
.catch((error:any)=>{
return Observable.throw({Error:error});
});
return this.cache.loadFromDelayedObservable(url, req,'user',ttl,delay);
}
正确的方向将受到高度赞赏。 的由于