-
-
Notifications
You must be signed in to change notification settings - Fork 5k
Description
This request is for 2.0.
The non-exact route matching strategy is good, so that we can define a router-link
as the entrance of one module which contains several subroutes and the router-link
remains active when we are navigating in this module. It is common that this router-link
is defined as a redirect to some default subroute, e.g.:
var router = new VueRouter({
routes:[{
path: '/entry', redirect: '/entry/subroute'
}, {
path: '/entry/subroute', component: SubRoute
}, {
path: '/entry/anothersubroute', component: AnotherSubRoute
}]
})
<router-link to="/entry">Entry</router-link>
<router-link to="/anothermodule">AnotherModule</router-link>
Every time we click the Entry
link , we go to SubRoute
, even if last time when we left Entry
module we were at AnotherSubRoute
.
If we want the router-link
to be smart enough to go to the last subroute we visit in Entry
module, we must first save where we were in Entry
module some place, such as a store.
var store = {
last_entry_route: ''
}
and update it whenever we enter a subroute.
Currently we can use an extra component to achieve the dynamic behavior:
const DynamicRedirect = Vue.extend({
render(){
this.$router.replace(store.last_entry_route)
}
})
and use it like this:
{path: '/entry', component: DynamicRedirect}
But this is quite a bit of code, and doing just a route redirect in render
function does not feel quite right. I wish we can have a function as the redirect
target of a route, i.e.
{path:'/entry', redirect: () => store.last_entry_route}