Vue路由遇到的问题

89 阅读1分钟

1、当params结合query使用时

{ path: '/home/:keyWord?', name: 'Search', }       // 路由配置中在params后?

2、params有可能传递空字符串或不传值

this.$router.push({		
    name: 'Search',
    params: { '' || undefined },                    // 路由使用时加上||undefined
    query: { k: this.keyWord }
})

3、需要传props时

{ path: '/search', name: 'Search', props: () => { return keyWord: this.$route.params.keyWord }}

4、编程式导航多次传一样的参数报错,重写路由中 VueRouter.prototype.push

import VueRouter from 'vue-router'							
Vue.use(VueRouter)
let originPush = VueRouter.prototype.push						
VueRouter.prototype.push = function (location, res, rej) {
  if (res && rej) {
    originPush.call(this, location, res, rej)
  } else {
    originPush.call(this, location, () => {}, () => {})
  }
}