【问题标题】:Back button doesn't detect created() on vuejs后退按钮未检测到 vuejs 上的 created()
【发布时间】:2020-04-23 01:14:12
【问题描述】:

所以我创建了一些代码

created() {
    if(!this.$session.exists() && !this.exists) {
      if(this.$router.currentRoute.path != '/login'){
        this.loginAlert = true
        this.$router.push('/login') 
      }
    }else{
      this.login = this.$session.get('uname')
      this.exists = true
    }
  },

当我刷新我的页面时,这是有效的

我的 vue 是 localhost:8081

所以当我打开 localhost:8081 时,它会提示尚未登录并重定向到 localhost:8081/login。但是在重定向之后,我按下浏览器上的返回按钮,localhost:8081 链接是打开的,并且警报没有出现,所以它就像绕过

我该如何解决这个问题?

【问题讨论】:

  • 请解释一下你想要实现什么,当created实际运行在vue生命周期(it.vuejs.org/v2/guide/instance.html#Lifecycle-Diagram)时,您似乎没有正确阅读。我想你可以解决你在路由器中移动这个逻辑并在那里检查会话有效性的问题,但很难说,因为我们不知道你想要实现什么。
  • 因此,如果未设置会话,则用户无法访问 localhost:8081/。他们必须先登录 localhost:8081/login。

标签: vue.js back


【解决方案1】:

您的组件只创建一次,因此重定向后不会再次调用。您可以尝试在您的<router-view> 上添加:key

<router-view :key="$route.fullPath">

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2016-12-08
  • 2018-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-11
  • 2021-01-19
相关资源
最近更新 更多