【问题标题】:How to redirect to home page after logout in nuxt?nuxt注销后如何重定向到主页?
【发布时间】:2019-07-02 18:38:31
【问题描述】:

登录网站后,当我点击特定按钮时,用户从网站注销到主页。

这是我的template 代码:

<template>
  <nav id="header" class="navbar navbar-expand header-setting">
    <div class="main-content">
          <div class="notification" @click="LogOut()"></div>
      </div>
    </div>
  </nav>
</template>

这是我的script 代码:

export default {
  name: 'HeadeAfterLogin',
  methods: {
    LogOut() {
      localStorage.removeItem('token')
    }
  }
}

谁能帮我完成LogOut功能?

【问题讨论】:

    标签: vue-router nuxt.js


    【解决方案1】:

    在您的组件中,您可以访问this.$router

    所以你可以轻松做到:

    export default {
      name: 'HeadeAfterLogin',
      methods: {
        LogOut() {
          localStorage.removeItem('token')
          this.$router.push('/')
        }
      }
    }
    

    【讨论】:

    • 可能想用 this.$router.replace 代替 push
    • @PirateApp 为什么?
    • @Čamo 如果用户点击浏览器上的返回按钮,他们将返回他们登录的页面,不是吗?
    【解决方案2】:

    我正在做的是:

    组件:

    ### html 
    <a href="#" class="dropdown-item" @click.prevent="signOut">LogOut</a>
    
    ### script
    export default {
        methods: {
          signOut() {
            this.$auth.logout();
          }
        }
    }
    

    nuxt.config.js

    auth: {
        strategies: {
            ...
        },
        redirect: {
            login: '/login',
            logout: '/login', # after logout, user will be redirected here.
            home: '/'
        },
    }
    

    【讨论】:

      猜你喜欢
      • 2015-08-09
      • 2014-02-10
      • 2021-10-18
      • 2017-08-22
      • 2020-04-16
      • 1970-01-01
      • 1970-01-01
      • 2011-07-08
      • 2014-08-16
      相关资源
      最近更新 更多