vue-router.js

    1. <body>
    2. <!-- 步骤一: vue之后导入vue-router路由插件 -->
    3. <script src='https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js'></script>
    4. <script src="./vue-router.js"></script>
    5. <div id='myApp'>
    6. <!-- 步骤二: 在根组件模板中,添加路由跳转标签 -->
    7. <router-link to="/">主页</router-link> |
    8. <router-link to="/list">列表</router-link> |
    9. <router-link to="/user">个人</router-link>
    10. <!-- 步骤六: 在根组件模板中, 添加路由出口: 路由组件渲染的位置 -->
    11. <router-view></router-view>
    12. </div>
    13. <!-- 列表页的组件模板 -->
    14. <template id="listTem">
    15. <div>
    16. <router-link to="/rank">段位排行榜</router-link>|
    17. <router-link to="/list/power">战力排行榜</router-link>|
    18. <router-link to="/score">积分排行榜</router-link>
    19. <!-- 二级路由的路由出口 -->
    20. <router-view></router-view>
    21. </div>
    22. </template>
    23. <script>
    24. // 步骤三: 创建路由控制分发跳转的组件
    25. var MainPage = Vue.component("mainCom", {template: "<h1>这是主页</h1>"});
    26. var ListPage = Vue.component("listCom", {template: "#listTem"});
    27. var UserPage = Vue.component("userCom", {template: "<h1>这是用户页</h1>"});
    28. var RankPage = Vue.component("rankCom", {template: "<h1><ol><li>这是段位列表</li></ol></h1>"});
    29. var PowerPage = Vue.component("powerCom", {template: "<h1><ol><li>这是战力列表</li></ol></h1>"});
    30. var ScorePage = Vue.component("scoreCom", {template: "<h1><ol><li>这是积分列表</li></ol></h1>"});
    31. // 步骤四: 创建路由对象,配置路由信息
    32. const router = new VueRouter({
    33. routes:[
    34. {path: "/", component: MainPage},
    35. {
    36. path: "/list",
    37. component: ListPage,
    38. // 配置二级路由
    39. children: [
    40. {path: "/rank", component: RankPage},
    41. // 二级路不加/时,使用一级路由调用 /list/power
    42. {path: "power", component: PowerPage},
    43. {path: "/score", component: ScorePage},
    44. {path: "/", redirect: '/rank'}
    45. ]
    46. },
    47. {path: '/user', component: UserPage}
    48. ]
    49. })
    50. new Vue({
    51. el: '#myApp',
    52. // 步骤五: 把路由对象加入vue对象中
    53. router,
    54. data: {}
    55. })
    56. </script>
    57. </body>