路由

  • 路由map
  • 路由視圖
  • 路由導(dǎo)航

實(shí)現(xiàn)簡(jiǎn)單路由

import VueRouter from 'vue-router'
Vue.use(VueRouter)

let router = new VueRouter({
  mode: 'history',//這樣訪問(wèn)路由就不需要加上/#
  routes: [
    {
      path: '/a',
      component: a
    },
    {
      path: '/b',
      component: b
    }
  ]
})
new Vue({
  router,
  el: '#app',
  render: h => h(App),
  directives: {

  }
})

路由的代碼顯示在

<router-view></router-view>
<router-link :to="{path:'a'}">to a</router-link>//可以實(shí)現(xiàn)轉(zhuǎn)換成a標(biāo)簽跳轉(zhuǎn)
<router-link :to="{path:'b'}">to b</router-link>

實(shí)現(xiàn)多層參數(shù)路由

let router = new VueRouter({
  mode: 'history',//這樣訪問(wèn)路由就不需要加上/#
  routes: [
    {
      path: '/a/:color/detail/:type',
      component: a
    },
    {
      path: '/b',
      component: b
    }
  ]
})

此處
/a/:color/detail/:type
帶冒號(hào)的表示屬性為color,填的任何值都會(huì)賦值給color和type

路由嵌套

routes: [
    {
      path: '/a',
      component: a,
      children: [
        {
          path: 'aa',
          component: aa
        }
      ]
    },
    {
      path: '/b',
      component: b,
      children: [
        {
          path: 'bb',
          component: bb
        }
      ]
    }
  ]

子路由渲染在父路由的組件里,所以需要在父路由的組件加上
<router-view></router-view>
同時(shí)我們也可以通過(guò)
<router-link :to="{path:'b/bb'}">to b bb</router-link>
或者通過(guò)代碼實(shí)現(xiàn)

// 字符串
router.push('home')

// 對(duì)象
this.$router.push({path: '/login?url=' + this.$route.path});

// 命名的路由
router.push({ name: 'user', params: { userId: 123 }})

// 帶查詢參數(shù),變成/backend/order?selected=2
this.$router.push({path: '/backend/order', query: {selected: "2"}});

重定向

在routes中加上

{
      path: '/',
      redirect: '/a'
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容