IT이야기

페이지 로드를 계속 허용하는 Vue 라우터 가드 방법

cyworld 2022. 3. 26. 16:33
반응형

페이지 로드를 계속 허용하는 Vue 라우터 가드 방법

쿠키에 토큰이 저장되지 않은 상태에서 사용자가 페이지를 입력하지 못하도록 하기 위해 다음과 같은 보안 검색대를 사용하고 있다.그러나 사용자가 페이지를 충분히 입력할 수 있지만 구성요소가 로드되지 않으면 빈 페이지일 뿐이다.

이런 일이 일어나지 않게 하려면 라우터 파일에 또 무엇을 추가해야 할까?

여기 나의 경계 상수가 있다.

const guard = function(to, from, next) {
  const token = Cookies.get('token')
  if(typeof token === 'undefined' || token === null ){
    this.$store.dispatch('logout')
    window.location.href = "/";
  } else {
    next();
  }
};

사용 시도next('/')대신에window.location.href = "/":

import store from 'path/to/store/
const guard = function(to, from, next) {
  const token = Cookies.get('token')
  if(typeof token === 'undefined' || token === null ){
   store.dispatch('logout');
    next('/');
  } else {
    next();
  }
};

참조URL: https://stackoverflow.com/questions/61409829/vue-router-guard-method-still-allowing-page-load

반응형