我正在用Vue重写现有的Angular 1应用程序.
该应用程序总是需要由用户进行身份验证locale,id并token进入任何意见之前.尊重API的约定,我token在主要父路由中指定了一个查询参数.
来自现有的Angular的UI路由器实现,我认为这是要走的路:
// main.js
new Vue({
el: '#app',
router,
store,
template: '<router-view name="main"></router-view>'
})
// router.js
const router = new Router({
mode: 'history',
routes: [
{
name: 'start',
path : '/:locale/:id', // /:locale/:id?token didn't work
query: {
token: null
},
beforeEnter (to, from, next) {
// 1. Get data from API via locale, id and token
// 2. Update store with user data
},
components: {
main: startComponent
},
children: [{
name: 'profile',
path: 'profile',
components: {
main: profileComponent
}
}]
}
]
})
Run Code Online (Sandbox Code Playgroud)
当我浏览到的profile观点,我希望更改视图和查询令牌留下来,如/en-US/123?token=abc来/en-US/123/profile?token=abc.都没有发生.
我正在使用Vue 2.3.3和Vue Router 2.3.1.
问题:
mbr*_*int 18
您可以在Router的全局挂钩中解决此问题
import VueRouter from 'vue-router';
import routes from './routes';
const Router = new VueRouter({
mode: 'history',
routes
});
function hasQueryParams(route) {
return !!Object.keys(route.query).length
}
Router.beforeEach((to, from, next) => {
if(!hasQueryParams(to) && hasQueryParams(from)){
next({name: to.name, query: from.query});
} else {
next()
}
})
Run Code Online (Sandbox Code Playgroud)
如果新路由(to)没有自己的参数,那么它们将从前一个路由中获取(from)
| 归档时间: |
|
| 查看次数: |
5769 次 |
| 最近记录: |