I have a problem, which seemed to be simple but now is not so simple (for me):
I have set up a Vue project with the vue-cli (Router, VueX, PWA). I have set up some routes as always (following straight the recommendations of the documentation) and some state-fields in VueX:
import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '@/views/Home.vue'
import Login from '@/views/Login.vue'
import Logout from '@/components/functional/Logout.vue'
import Datapoints from '@/views/Datapoints.vue'
import store from '../store'
Vue.use(VueRouter)
const routes = [
{
path: '/',
name: 'home',
component: Home,
meta: {
requiresAuth: true
}
},
{
path: '/login',
name: 'login',
component: Login,
meta: {
requiresGuest: true
}
},
{
path: '/logout',
name: 'logout',
component: Logout
},
{
path: '/project/:id',
name: 'project',
component: Datapoints
}
]
const router = new VueRouter({
mode: 'history',
routes
})
router.beforeEach((to, from, next) => {
if (to.matched.some(record => record.meta.requiresAuth)) {
if (!store.getters.isAuthenticated) {
next({
path: '/login',
query: { redirect: to.fullPath }
})
} else {
next()
}
} else {
next()
}
})
router.beforeEach((to, from, next) => {
if (to.matched.some(record => record.meta.requiresGuest)) {
if (store.getters.isAuthenticated) {
next({
path: '/',
query: { redirect: to.fullPath }
})
} else {
next()
}
} else {
next()
}
})
export default router
In my Views / Components, i use the push
method on the $router
instance, to rote programatically, like this:
this.$router.push({ name: 'home', params: { status: project.response.status, message: project.response.data.error }})
, where project
is the result of an awaited
axios HTTP request.
My Problem
Every time i push programatically a new route, or use the router-view
element, my page reloads (what i want to prevent in a SPA / PWA ...)
My Vue instance:
new Vue({
router,
store,
vuetify,
render: h => h(App)
}).$mount('#app');
I would be happy if anynoe could help me out to not reload the page on every route change with the Vue router.
.push({ name: 'home', params: { status: project.response.status, message: project.response.data.error }})
be identical to result in navigation torouter.push({ path: `/${project.response.status}/${project.response.data.error}`)
– Bravostatus
andmessage
params on your home route – Lorin Ionitaproject.response.status
. Axios returns the response, so it should beresponse.status
or if you assign the response toproject
,project.status
in your case – Lorin Ionita