Programing

Vuex store getter는 항상 false를 반환함

c10106 2022. 4. 14. 21:34
반응형

Vuex store getter는 항상 false를 반환함

나는 Vue js와 Vuex를 처음 접한다.가게 주인에게 접근하는 데 어려움을 겪고 있지만

Getter에서 볼 수 있고, Activated는 진실이고, In My getter in store.js는

 getters: {
loggedIn(state) {
  return state.token !== null
},
Activated(state) {
  return state.activated;
},

활성화된 상태도 볼 수 있지만

store.getters.Activated

항상 거짓으로 돌아가지만 같은 기능으로 로그인한 다른 getter에 접속하면 값이 true가 된다.

store.getters.loggedIn

내가 접근하려고 했던 곳

router.beforeEach((to, from, next) => {
if (to.matched.some(record => record.meta.requiresAuth)) {
  if (!store.getters.loggedIn) {
    next({
      name: 'home',
    })
  } else {
    next()
  }
} 
else if (to.matched.some(record => record.meta.requiresActivation)) {
  debugger;
  if (store.getters.Activated===true) {
    next()
  } else {
    next({
      name: 'activate',
    })
  }
}
else if (to.matched.some(record => record.meta.requiresVisitor)) {
  if (store.getters.loggedIn) {
    next({
      name: 'activate',
    })
  } else {
    next()
  }
} else {
  next()
}

})

상점 js의 돌연변이

mutations: {
updateFilter(state, filter) {
  state.filter = filter
},

retrieveToken(state, token) {
  state.token = token
},
CheckActivation(state, value) {
  state.activated = value
},
destroyToken(state) {
  state.token = null
}},

그리고 행동들

actions: {
CheckActivation(context){
  if(this.token===null){
    context.commit('CheckActivation', false)
    return false;
  }
  else{
    var settings = {
      "async": true,
      "crossDomain": true,
      "url": "http://vuejs.test/api/user",
      "method": "GET",
      "headers": {
        "Accept": "application/json",
        "Authorization": "Bearer "+ this.state.token,
        "cache-control": "no-cache"
      }
    }
    $.ajax(settings).done(function (response) {
      if(response.verified)
      context.commit('CheckActivation', true)
      else
      context.commit('CheckActivation', false)
    });
  }
},

제발 도와주세요.

그래서 아마도 그것은 당신이 가게 변수가 true로 설정되기 전에 접근하고 있기 때문일 것이다.정확한 솔루션을 위해 위 변수의 돌연변이 코드와 메서드 콜을 추가하십시오.저장 작업 및 vue 파일의 메서드 호출에 비동기 대기 시간을 사용해야 할 수 있음.

참조URL: https://stackoverflow.com/questions/54478164/vuex-store-getter-always-returning-false

반응형