2016-06-22 1 views
0

gmaps api를 비동기 적으로로드하는 서비스가 있습니다. 로드 할 때 해당 서비스에 "isGoogleMapLibraryLoaded"속성 이름을 true로 저장합니다.경로 변경시 Angular2 Service가 다시 인스턴스화됩니다.

경로를 새로 고침하지 않고 이전으로 돌아 가면 서비스가 다시 인스턴스화되고 "isGoogleMapLibraryLoaded"속성이 손실됩니다.

경로를 전환 할 때 지속되는 서비스에서 속성을 설정하려면 어떻게해야합니까?

내 서비스 : (개인 맵 서비스 : GoogleMapService) 내가 부트 스트랩에서 서비스를 추가하고 내가 구성 요소 생성자에서

생성자를 주입하고있어

import { Injectable } from '@angular/core'; 

const GOOGLE_MAPS_API_KEY = 'AIzaSyDvo543530SU_xsZLvZ6SjTFbt1FPW9FI'; 
const URL = `https://maps.googleapis.com/maps/api/js?key=${GOOGLE_MAPS_API_KEY}&callback=__onGoogleMapLoaded`; 

@Injectable() 
export class GoogleMapService { 
    /** 
    * Google maps loading status (althought loading is not completed) 
    */ 
    private isGoogleMapLibraryLoaded: Boolean = false; 
    constructor(){ 
    } 

    getNewMapInstance(element, props){ 
    console.log(this.isGoogleMapLibraryLoaded); 
    this._loading().then(() => { 
     return new google.maps.Map(element, props); 
    }); 
    } 

    private _loading(){ 
    return new Promise((resolve, reject) => { 
     if(!this.isGoogleMapLibraryLoaded){ 
     this.isGoogleMapLibraryLoaded = true; 

      resolve(); 

     }else{ 
     resolve(); 
     } 
    }); 
    } 
} 

{}

ngAfterViewInit() { 
    this.googleMapInstance = this.mapService.getNewMapInstance(this.googleMapWrapperElement.nativeElement, { 
     center: {lat: -34.397, lng: 150.644}, 
     scrollwheel: false, 
     zoom: 8 
    }); 
    } 

답변

0

을 그 귀하가 서비스를 제공하는 지역에 따라 다릅니다. 루트 구성 요소에서 제공하는 경우 인스턴스는 루트 구성 요소 (각도 응용 프로그램이 실행되는 동안 동일)만큼 존재합니다.

라우트 된 구성 요소에 제공하면 구성 요소가 제거 되 자마자 폐기됩니다.

관련 문제