7

I have an app with a Login.vue and Home.vue files. Because I converted an admin HTML website to a vue 3 app, my javascript only works with page reload. When creating the app I selected add router for SPA maybe I shouldn't have. Up to this point, the views are working except when I redirect from login to home without reloading. Since it is not reloading, my navbar or any JS-dependent functions won't work. how do I redirect from login to home with page reload? Currently, I have the below code but still not working.

this.$router.push({
      path: "/admin/home",
      reload: true
    });

2 Answers 2

13

You can use this.$router.go(0) to reload the page. You need the 0 if you use Typescript as mentioned by @Second2None. In combination with this.$router.push({ path: '/some/path' }) you can achieve it without directly changing the browser's window property.

<template>
  <button @click="redirectReload">Redirect & Reload</button>
</template>
    
<script>
  export default {  
    methods: {
      redirectReload() {
        this.$router
          .push({ path: '/expedition' })
          .then(() => { this.$router.go(0) })
      }
    }
  }
</script>

Notice how I used .then after $router.push(). Because the push() method is asynchronous, the reload would happen before the route change without then().

As a bonus, it lets you use all the features of $router.push (for example using arguments like { name: 'home' }.


Example with Composition API and async/await syntax:

<script setup>

import { useRouter } from 'vue-router'
const router = useRouter()

const redirectReload = async () => {
  await router.push({ path: '/expedition' })
  router.go(0)
}

</script>

Update

I recommend checking out this thread as there are answers that describe how to reload the page component without reloading the whole website using the :key prop and other useful information.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for the response did the trick.
I spent a couple hours troubleshooting why the template section of my login page wasn't rendering when I redirected to it using <RouterLink to="/login">Continue to the login page</RouterLink>. This solved my issue. I don't understand why but it works
For anyone using 'script setup' I did it like this: import { useRouter } from 'vue-router'; const router = useRouter(); const redirectReload = ()=> { router .push({ path: '/login' }) .then(()=> { router.go() }) }
@ToddHammer your app probably had some problems with component update order. Better to fix that without using crutches like reloading the whole SPA. You better create a new question with more details and ping me
If you are using typescript router.go() throws an error: Expected 1 arguments, but got 0.ts(2554) to fix this you can just do router.go(0) for reload.
3

Vue Router not reload page when you navigate to new URL.

You can try this code for you issue:

const url = new URL('/admin/home', window.location.origin)
window.location.href = url.toString()

Hope this help

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.