解決多個路由綁定同一個組件 獲取參數只獲取一次的方法

{
    path: '/application',
    title: '個人工做',
    icon:'code-working',
    name: 'application',
    component: Main,
    children: [
      {
        path: 'index/:id', title: '個人申請', name: 'myApplication', component: resolve => {
          require(['@/views/application/index'], resolve);
        }
      },
      {
        path: 'index/:id', title: '個人待辦', name: 'have not done', component: resolve => {
          require(['@/views/application/index'], resolve);
        }
      },
      {
        path: 'index/:id', title: '個人已辦', name: 'have been done', component: resolve => {
          require(['@/views/application/index'], resolve);
        }
      }
    ]
  },

這三個路由綁定的是同一個組件,在javascript

created(){
   console.log(this.$route.params.id)
}

裏面這種動做只會執行一次,也就是隻能拿到該組件建立時的路由id,
若是要得到不一樣的id必須使用官方推薦的方法
響應路由參數的變化html

const User = {
  template: '...',
  watch: {
    '$route' (to, from) {
      // 對路由變化做出響應...
    }
  }
}

或者使用 2.2 中引入的 beforeRouteUpdate 守衛:vue

const User = {
  template: '...',
  beforeRouteUpdate (to, from, next) {
    // react to route changes...
    // don't forget to call next()
  }
}
相關文章
相關標籤/搜索