2017-10-11 10 views
2

라고 안한다 나는 computed 방법이 뷰 계산 방법은

<GMap class="c-splitview__map" :markers="getMarkers" :position="currentPosition" :zoom="currentZoom" v-if="currentPosition" /> 
<div class="m-storefinder__placeholder" v-else> 
    <h1 class="o-headline">{{$tc("storefinder.emptyDeeplink")}}</h1> 
</div> 

그리고 어떤 이유로

, 그것은 처음 호출되는 경우, 어떻게 작동해야합니까? 그러나 다시 호출 (Vue 구성 요소 다시 렌더링)하려고하면 호출되지 않습니다.

BUT!

는 언제과 같이 첫 번째 if() 문을 주석 : 그것은 어떻게 다시해야 작동

computed: { 
    currentPosition() { 
    // if(this.get_local_storage_state()){ 
    // return this.lastLocation 
    // } 

    if (this.currentRestaurant) { 
     return this.currentRestaurant.address.location 
    } else if (this.searchPosition.lat && this.searchPosition.lon) { 
     return this.searchPosition; 
    } else { 
     return null; 
    } 
    } 
} 

.

this.get_local_storage_state() 기능은 다음과 같습니다과 methods:{}에 있습니다

get_local_storage_state(){ 
    let state = localStorage.getItem('McDonaldsStoreWasOpen'); 
    return state === "true" ? true : false; 
} 

나는 기본적으로 상태 관리 시스템으로 로컬 저장소를 사용하는 것을 시도하고있다.

+0

[localStorage 및 boolean 'string'] 가능한 복제본 (https://stackoverflow.com/questions/30644250/localstorage-and-boolean-string) – Terry

답변

5

localStorage은 반응성이 없어 관찰 할 수 없다. 계산 된 값이 이고 캐시 된 값이 인 결과와 함께 계산 된 로컬 저장소에서 값을 가져올 때 결과가 캐시되고 계산 된 값이 항상 같은 값을 반환한다는 것을 의미합니다. 그 이유는 localStorage에서 값을 제거 할 때 계산 된 이력서가 작동하는 이유이기도합니다.

localStorage의 데이터 값을 초기화하고 계산 된 값을 사용하고 나중에 지정된 시간에 localStorage에 값을 저장하는 것이 좋습니다.

차이점은 codepen demonstrating입니다.

+0

괜찮 았던가. localstorage의 문제는 알고 있지만 해결 방법이 확실하지 않은 경우 계산 된 메소드는 내가 주석 처리하고있는'if()'문장의 결과에 의존하기 때문에 당신이 제안한 것은이 상황에 적용된다. – Jousi

+0

@ Jousi 반응 예제가 로컬 저장 값 대신 데이터 값에서 작동하는 계산을 사용하도록 예제 펜을 업데이트했습니다. – Bert