I found how to Silently update url
, but route anyway is triggered (but this maybe is not a problem - dependents on situation).
In my case user may create a profile, for example via URL http://localhost:8080/#/user/create/
, provide some profile info and once "Save" is clicked user is redirected to his/her created profile with URL for example http://localhost:8080/#/FaFYhTW9gShk/
, where the user may continue editing the profile form.
How do I do that?
user/create/
page (ProfileCreate) and FaFYhTW9gShk/
page (ProfileDetails) use the same ProfileUser
component.
// router.js
const routes = [
{
path: '/user/create',
component: Profile,
children: [
{
path: '',
name: 'ProfileCreate',
component: ProfileUser
}
]
}
{ path: '/:profileUrl',
component: Profile,
children: [
{
path: '',
name: 'ProfileDetails',
component: ProfileUser
}
]
}
]
- In
router.beforeEach
I check whether I need to load data from the back end, or I need simply to change URL?
// router.js
router.beforeEach((to, from, next) => {
if (!('isProfileLoaded' in to.params)) {
// load profile from backend and update Vuex
}
next()
})
- When "Save" button is clicked, in
action
I send data to backend + call router.push
// action.js
import router from '@/router'
// Here I send data to backend
// Some code
// Update-URL-Trick
router.push({
name: 'ProfileDetails',
params: { profileUrl: profileUrl isProfileLoaded: true }
})
As far as isProfileLoaded
is provided, Vue re-uses profile's data from Vuex + router silently
updates URL re-using the same ProfileUser
component.
onComplete
callback. – Lovesick