programing

VueJS + VueRouter:조건부로 경로 비활성화

prostudy 2022. 4. 8. 22:13
반응형

VueJS + VueRouter:조건부로 경로 비활성화

나는 VueRouter의 루트를 어떻게 조건부로 비활성화해야 더 이상 접속할 수 없는지 궁금하다!

로 방향을 바꾸려고 했다.this.$router.replace('/')URL에 내가 건너뛰고 싶은 경로가 나와있었어

생각나는 거 있어?

편집:

제 VUEX-Store 입니다.router.replace('/')

const store = new Vuex.Store({
  state: {
    users: [ ],
    friendships: [ ],
    userID: null
  },
  mutations: {
    signUp(state, payload) {
      auth.createUserWithEmailAndPassword(payload.email, payload.password).then((user) => {
        if (user !== null) {
          state.userID = user.uid
          router.replace('/')
        }
        else {
          state.userID = null
        }
      })
    },
    signIn(state, payload) {
      auth.signInWithEmailAndPassword(payload.email, payload.password).then((user) => {
        if (user !== null) {
          state.userID = user.uid
          router.replace('/')
        }
        else {
          state.userID = null
        }
      })
    },
    signOut(state) {
      auth.signOut()
      state.userID = null
      router.replace('/signin')
    },
    authenticate(state) {
      auth.onAuthStateChanged((user) => {
        if (user !== null) {
          state.userID = user.uid
          router.replace('/')
        }
        else {
          state.userID = null
        }
      })
    }
  },
  actions: {
    signUp({ commit }) {
      commit('signUp')
    },
    signIn({ commit }) {
      commit('signIn')
    },
    signOut({ commit }) {
      commit('signOut')
    },
    authenticate({ commit }) {
      commit('authenticate')
    },
    redirect({ commit }) {
      commit('redirect')
    }
  }
})

그리고 여기 내 구성 요소가 있다.

<template>
  <div id="you">
    <h1>you</h1>
    <p>You are on your secret page!</p>
    <p>{{ $store.state.userID }}</p>
  </div>
</template>

<script>
  export default {
    name: 'you',
    beforeCreate() {
      if (this.$store.state.userID === null) {
        this.$router.replace('/signin')
      }
    }
  }
</script>

다음과 같이 조건부로 비활성화하려는 경로에 메타 패럴을 추가할 수 있다.

export const routes = [
  {path: '/', component: foo},
  {path: '/bar', component: bar, meta:{conditionalRoute:true}}
]; 

그리고 사용router.beforeEach당신의 본문에서.js:

router.beforeEach((to, from, next) => { 
    if (to.matched.some(record => record.meta.conditionalRoute)) { 
        // this route requires condition to be accessed
        // if not, redirect to home page. 
        if (!checkCondition) { 
            //check codition is false
            next({ path: '/'}) 
        } else { 
            //check codition is true
            next() 
        } 
    } else { 
        next() // make sure to always call next()! 
    } 
}) 

또는 다른 사용beforeRouteEnter() 그 경로의 구성요소에 있는 지역 내 항법 보호대

beforeRouteEnter(to, from, next){
    next(vm => { 
        // access to component instance via `vm` 
        if(checkCondition){
            next();
        }else{
            next('/');
        }
    })
} 

로그인 구성 요소

beforeRouteEnter(to, from, next){
    next(vm => { 
        // access to component instance via `vm` 
        if(vm.$store.state.userUID !== null){
            next('/');
        }else{
            next();
        }
    })
}  

경로에서 항법 가드를 사용하여 항로가 비활성화할 경로와 일치하는지 확인한 다음return실행 대신next()

router.beforeEach(to, from, next) {
  if (to.path === "yourRoute") {
    return;
  }
}

참조URL: https://stackoverflow.com/questions/44386943/vuejs-vuerouter-conditionally-disabling-a-route

반응형