vue-router 用法和技巧 hash模式

使用
// router.js
import Router from 'vue-router'

Vue.use(Router)
export default new Router({
  mode: 'hash', // 模式:hash | history | abstract
  base: process.env.BASE_URL, // http://localhost:8080
  routes: [
    {
      path: '/',
      name: 'home',
      component: Home,
      children: [
        { path: "/list", name: "list", component: List },
        { path: "detail/:id", component: Detail, props: true },
      ]
    },
    {
      path: '/about',
      name: 'about',
      meta: { auth: true },
      // 路由層級代碼分割
      // 生成分片(about.[hash].js)      
      // 當路由房問時會懶加載.
      component: () => import(/* webpackChunkName: "about" */ './views/About.vue')
    }
  ]
})
配置
import Home from './views/Home.vue'

const routes = [
  {
    path: '/',
    name: 'home',
    component: Home,
    children: [
      { path: "/list", name: "list", component: List },
      { path: "detail/:id", component: Detail, props: true },
    ]
  },
  {
    path: '/about',
    name: 'about',
    meta: { auth: true },
    // 路由層級代碼分割
    // 生成分片(about.[hash].js)      
    // 當路由房問時會懶加載.
    component: () => import(/* webpackChunkName: "about" */ './views/About.vue')
  }
]
指定路由器
// main.js
new Vue({
  router,
  render: h => h(App)
}).$mount("#app");
路由視圖
<router-view/>
導(dǎo)航鏈接
<router-link to="/">Home</router-link>
<router-link to="/about">About</router-link>
路由嵌套

應(yīng)用界面通常由多層嵌套的組件組合而成。同樣的,URL中各段動態(tài)路徑也按某種結(jié)構(gòu)對應(yīng)嵌套的各層組件。

  • 創(chuàng)建List.vue
  • 配置路由,router.js
{
  path: '/',
  component: Home,
  children: [{ path: '/list', name: 'list', component: List }]
}
  • 在Home.vue中添加插座
<template>
  <div class='home'>
    <h1>home</h1>
    <router-view/>
  </div>
</template>
路由守衛(wèi)

路由導(dǎo)航過程中有若干生命周期鉤子,可以在這里實現(xiàn)邏輯控制。

  • 全局守衛(wèi),router.js
// 守衛(wèi)
router.beforeEach((to, from, next) => {
  // 要訪問/about且未登錄需要去登錄
  if (to.meta.auth && !window.isLogin) {
    // next('/login') 
    if (window.confirm("請登錄")) {
      window.isLogin = true;
      next();   // 登錄成功,繼續(xù)  
    } else {
      next('/');// 放棄登錄,回首頁
    }
  } else {
    next(); // 不需登錄,繼續(xù)   
  }
});
  • 路由獨享守衛(wèi),home.vue
beforeEnter(to, from, next) {
  // 路由內(nèi)部知道自己需要認證
  if (!window.isLogin) {
    // ...
  } else {
    next()
  }
}
  • 組件內(nèi)守衛(wèi)
export default {
  beforeRouteEnter(to, from, next) {
    //this不能用    
  },
  beforeRouteUpdate(to, from, next) { },
  beforeRouteLeave(to, from, next) { }
}
動態(tài)路由
// 映射關(guān)系
const compMap = {
  'home': () => import('./views/Home.vue'),
  'about': () => import('./views/About.vue'),
}

// 遞歸替換
function mapComponent(route) {
  route.component = compMap[route.name]
  if (route.children) {
    route.children = route.children.map(child => mapComponent(child))
  }
  return route
}

// 異步獲取路由 
api.getRoutes().then(routes => {
  const routeConfig = routes.map(route => mapComponent(route))
  router.addRoutes(routeConfig)
})
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

  • 前言 vue-router是什么:是vue.js官方的路由管理器和vue.js的核心深度的集成,讓開發(fā)者更加簡單的...
    GUAN_one閱讀 3,858評論 0 2
  • 學(xué)習(xí)目的 學(xué)習(xí)Vue的必備技能,必須 熟練使用 Vue-router,能夠在實際項目中運用。 Vue-rout...
    _1633_閱讀 92,878評論 3 58
  • 路由,其實就是指向的意思,當我點擊頁面上的home按鈕時,頁面中就要顯示home的內(nèi)容,如果點擊頁面上的about...
    裘馬輕狂大帥閱讀 783評論 0 5
  • 1路由,其實就是指向的意思,當我點擊頁面上的home按鈕時,頁面中就要顯示home的內(nèi)容,如果點擊頁面上的abou...
    你好陌生人丶閱讀 1,773評論 0 6
  • 第一個 vue-router 路由 路由,其實就是指向的意思,當我點擊頁面上的home按鈕時,頁面中就要顯示hom...
    索倫x閱讀 2,524評論 0 3

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