VueJS + VueRouter:有条件地禁用路由

VueJS + VueRouter: Conditionally Disabling A Route

想知道如何有条件地禁用VueRouter中的路由,使其无法再访问!

我尝试使用 this.$router.replace('/') 重定向,但 URL 确实显示了我想跳过的路线。

有什么想法吗?

编辑:

这是我的 VUEX 商店:看看 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>

您可以将 a meta feild 添加到要有条件地禁用它的路由,如下所示:

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

并在 main.js 中使用 router.beforeEach :

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() navigation guard

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();
        }
    })
}  

在你的路由中,你可以使用navigation guard检查路由是否匹配你要禁用的路由然后return而不是执行next()

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