【问题标题】:Passing data between paths using $router.push() in VueJS在 VueJS 中使用 $router.push() 在路径之间传递数据
【发布时间】:2018-11-12 07:51:40
【问题描述】:

我对 VueJS 有点陌生,我试图在用户通过身份验证后将用户角色从登录路径传递到主页,然后我可以确定用户可以使用 v-if 看到哪些链接. 下面是我的 login.vue 代码:

this.$axios({
                      method: 'post',
                      url: 'api/role',
                      data: {
                        'email': this.username
                      },
                      headers: {'Authorization': 'Bearer '+ this.$tokens.getToken()}
                    })
                    .then(response => {
                        this.role = response.data['role'];
                        
                    })
    				this.$router.push('/');
    			})
    			.catch(function(error){
    				console.log(error);
    			})

如何将“this.role”值传递给路径“/”以便从那里访问它?

【问题讨论】:

标签: javascript vue.js vuejs2 vue-component vue-router


【解决方案1】:

您可以使用查询来传递角色。

所以添加一个查询,您可以在其中以编程方式将用户导航到主页:

this.$router.push({path:'/', query:{role: this.role}});

然后在您的主页组件中,您可以检索您的功能的路由查询

例如:

//homme page component

created(){
  if(this.$route.query.role === 'admmin){
    //this is admin
  }
}

【讨论】:

  • 感谢您的回答。但我实际上想避免通过 url 传递角色,因为用户可以轻松编辑 url 参数并访问受限页面。
  • 你知道如何避免通过参数@Kingsley 传递它吗?
  • Urrhh.. @Kelvin,我最终要做的是使用 Vue 状态管理工具 (Vuex) 来处理角色管理和其他敏感信息,例如我不想通过的令牌网址。
猜你喜欢
  • 2023-02-07
  • 2017-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-14
  • 1970-01-01
  • 1970-01-01
  • 2020-10-30
相关资源
最近更新 更多