How to redirect to home page after logout in nuxt?

14,916

Inside your component you have access to this.$router

So you can easily do:

export default {
  name: 'HeadeAfterLogin',
  methods: {
    LogOut() {
      localStorage.removeItem('token')
      this.$router.push('/')
    }
  }
}
Share:
14,916
Mohandes
Author by

Mohandes

Updated on June 04, 2022

Comments

  • Mohandes
    Mohandes almost 2 years

    after login in site i want when i click to specific button , user log out from site to home page.

    here is my template code:

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

    and here is my script code:

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

    any one can help me to complete LogOut function ?

  • PirateApp
    PirateApp over 3 years
    may wanna use this.$router.replace instead of push
  • Čamo
    Čamo over 2 years
    @PirateApp Why?
  • PirateApp
    PirateApp over 2 years
    @Čamo if the person hits back button on the browser they ll go back to the page where they were logged in, no?