2020年2月11日 星期二

[Vue] vue-router 進入頁面若無帶 query 參數帶入指定 query

情境: 在任意網頁甚至主頁點選進入 /products
會沒有指定 query ( 如 /products?type=food ) 只進入 /products
使其固定帶上 query 方式如下

{
name: 'products',
path: `${rootPath}/products`,
component: () => import(/* webpackChunkName: "courses" */ '../pages/products/').then(m => m.default || m),
beforeEnter: (to, from, next) => {
if (!to.query.type) {
next({
path: to.path,
query: { type: 'food' }
})
}
next()
}
},

沒有留言:

張貼留言