As I work on my cordova Vue app, I often encounter deeper links like this:
/profile/:id/:contact_type/contacts:contact_id
Usually, you would start on /profile/:id
and then navigate to
/profile/:id/:contact_type/contacts:contact_id
by clicking a link.
The standard router.go(-1)
function works well if you were on the profile page initially. However, if you come from a different page, such as /settings/:id
due to a notification or other trigger, the back button should function more like an Up button. Pressing back on
/profile/:id/:contact_type/contacts:contact_id
should take you to /profile/:id
, not /settings/:id
.
How can I achieve this? I've attempted various methods like splitting the current route by /
, removing the last segment, then rejoining and pushing to that route. However, this approach doesn't work well with parameters, especially when there are multiple ones.
const path = router.currentRoute.path
const URLSplit = path.split('/')
URLSplit.length = URLSplit.length - 1
const newTarget = URLSplit.join('/')
if (newTarget) {
router.push(newTarget)
} else {
router.push('/home')
}
I also explored using child routes, but that requires a router-view
in each page, which isn't ideal for my needs.
I have already intercepted the back button operation, but I am curious if there is a way to configure Vue to handle this type of back navigation automatically, or if there's a specific setup needed in the router, or perhaps a function that can determine the current route and navigate up to it?