index.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // 1.引入方法
  2. import { createRouter, createWebHashHistory, createWebHistory } from 'vue-router';
  3. import Home from '../views/Home.vue';
  4. import List from '../views/List.vue';
  5. import My from '../views/My.vue';
  6. const routes = [
  7. {
  8. path: "/",
  9. redirect: "/home"
  10. },
  11. {
  12. path: "/home",
  13. component: Home,
  14. name:'zhuye'
  15. },
  16. {
  17. path: "/list",
  18. component: List,
  19. name:'liebiao',
  20. redirect:'/list/demo2',
  21. children:[
  22. {
  23. path:'demo1',
  24. component:()=>import("../views/Demo/Demo1.vue")
  25. },
  26. {
  27. path:'demo2',
  28. component:()=>import("../views/Demo/Demo2.vue")
  29. },
  30. ]
  31. },
  32. {
  33. path: "/my",
  34. component: My
  35. },
  36. ]
  37. // 2.创建路由对象
  38. const router = createRouter({
  39. // 定义路由模式
  40. history: createWebHashHistory(),
  41. // 路由路径配置
  42. routes
  43. });
  44. // 3.导出路由对象
  45. export default router;
  46. // history hash区别
  47. // history:
  48. // 优点
  49. // 需要后端支持,刷新不会丢失路由信息
  50. // Url是更加美观 更接近传统网站的url
  51. // 缺点
  52. // 当后期项目上线 需要服务端配合处理路径问题 否则刷新会有404
  53. // hash:
  54. // 优点
  55. // 兼容性更好 不需要服务端处理
  56. // 缺点
  57. // url带有#号 不美观 在SEO优化方面相对较差