我想知道如何有条件地禁用VueRouter中的路由,这样就不能再访问它了!

我try 用this.$router.replace('/')重定向,但URL确实显示了我想跳过的路由.

有什么 idea 吗?

EDIT:

这是我的VUEXstore :看看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}}
]; 

在你的主菜单中使用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() 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();
        }
    })
}  

Vue.js相关问答推荐

当props 的值改变时如何更新类'

通过另一个文件调用模块化窗口组件

如何在 vuejs 中导入和使用本地 .csv 文件

如果 Vuetify 自动完成没有匹配的结果,则保留输入的文本

Vue CLI - 编译后将配置文件保留为外部

理解 VueJS 中的组件 props

如何在 vue.js 中删除类

查看 cli3 启用 CORS

如何将参数传递给使用 ...mapActions(...) 映射的函数?

Vuejs 2,VUEX,编辑数据时的数据绑定

Vuetify 单选按钮未显示为选中状态

为什么 vue 文档说 refs 不是响应式的,而实际上它们是响应式的

在Vue中单击路由链接上的激活方法

Leaflet+Vue+Vuetify / Leaflet map 隐藏 vuetify 弹出对话框

执行调度程序刷新期间未处理的错误,这可能是一个 Vue 内部错误

Vuetify RTL 风格

Vuejs 不会在 HTML 表格元素中呈现组件

我们可以像在 Angular 中一样创建 Vue.js 组件时将 HTML、JS 和 CSS 文件分开吗?

Apollo 客户端在向服务器发送Mutations 时返回400(错误请求)错误

在 Vue 中使用事件修饰符 .prevent 提交表单而不进行重定向