0
我有這個http.get請求,但它被觸發兩次。它在我的構造函數中。http.get被調用兩次
import { Component } from '@angular/core';
import { NavController, LoadingController } from 'ionic-angular';
import { Auth, User, IDetailedError } from '@ionic/cloud-angular';
import { Http, URLSearchParams, Headers } from "@angular/http"
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/share';
import { DetailsPage } from '../details/details';
import { LoginPage } from '../login/login';
@Component({
selector: 'page-dashboard',
templateUrl: 'dashboard.html'
})
export class Dashboard {
visitors: any;
currentSite: any;
searchDate: String = new Date().toISOString();
constructor(public navCtrl: NavController, public http: Http, public loadingCtrl: LoadingController, public user: User, public auth: Auth) {
if (this.auth.isAuthenticated()) {
this.currentSite = this.user.get('siteName',0);
let loading = this.loadingCtrl.create({
spinner: 'bubbles',
content: 'Fetching visitors ...',
duration: 5000
});
loading.present();
var headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
headers.append('Authorization', 'Basic '+ btoa(this.user.get('siteID',0)+':'+this.user.get('token',0)));
this.http.get('http://api.domain.com/visitors?search_from='+this.searchDate+'&search_to='+this.searchDate, {headers:headers}).map(res => res.json()).share().subscribe(data => {
this.visitors = data.res;
loading.dismiss();
});
} else {
this.navCtrl.setRoot(LoginPage);
}
}
基於Angular2 http.post gets executed twice它看起來像我需要包括股份(),但是當我嘗試這個,我得到一個錯誤..
如果我放在最後的份額()然後我得到「屬性不不存在類型的訂閱「,如果我把它放在訂閱之前,然後我得到」屬性共享不存在類型observable「
有人可以告訴我share()應該去哪裏...或者我是否實際上有不同問題?
認沽份額兩個請求後,地圖'.MAP(RES => res.json())。份額()。潛艇...' –
這使我....屬性共享不存在類型observable –
Chris
我認爲你有一個不同的問題。您的請求網址看起來好嗎?您的授權標頭設置是否正確?嘗試將console.log添加到您的構造函數中,以查看構造函數是否未執行兩次。 –