1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
    7. <title>Document</title>
    8. </head>
    9. <body>
    10. <div id="app">
    11. <h1>Hello App!</h1>
    12. <p>
    13. <!-- 使用 router-link 组件来导航. -->
    14. <!-- 通过传入 `to` 属性指定链接. -->
    15. <!-- <router-link> 默认会被渲染成一个 `<a>` 标签 -->
    16. <router-link to="/">首页</router-link>
    17. <router-link to="/student">会员管理</router-link>
    18. <router-link to="/teacher">讲师管理</router-link>
    19. </p>
    20. <!-- 路由出口 -->
    21. <!-- 路由匹配到的组件将渲染在这里 -->
    22. <router-view></router-view>
    23. </div>
    24. <script src="vue.min.js"></script>
    25. <script src="vue-router.min.js"></script>
    26. <script>
    27. // 1. 定义(路由)组件。
    28. // 可以从其他文件 import 进来
    29. const Welcome = { template: '<div>欢迎</div>' }
    30. const Student = { template: '<div>student list</div>' }
    31. const Teacher = { template: '<div>teacher list</div>' }
    32. // 2. 定义路由
    33. // 每个路由应该映射一个组件。
    34. const routes = [
    35. { path: '/', redirect: '/welcome' }, //设置默认指向的路径
    36. { path: '/welcome', component: Welcome },
    37. { path: '/student', component: Student },
    38. { path: '/teacher', component: Teacher }
    39. ]
    40. // 3. 创建 router 实例,然后传 `routes` 配置
    41. const router = new VueRouter({
    42. routes // (缩写)相当于 routes: routes
    43. })
    44. // 4. 创建和挂载根实例。
    45. // 从而让整个应用都有路由功能
    46. const app = new Vue({
    47. el: '#app',
    48. router
    49. })
    50. // 现在,应用已经启动了!
    51. </script>
    52. </body>
    53. </html>