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. <script src="./lib/vue.js"></script>
    9. <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
    10. </head>
    11. <body>
    12. <div id="app">
    13. <a href="#/login" >登陆</a>
    14. <a href="#/register" >注册</a>
    15. <router-view></router-view>
    16. <!--router-link标签上面a标签效果一样 -->
    17. <router-link to="/login" tag="span">登录</router-link>
    18. <router-link to="/register">注册</router-link>
    19. </div>
    20. <script>
    21. var register = {
    22. template: "<div><h1> 注册页面</h1></div >"
    23. }
    24. var login = {
    25. template: "<div><h1> 登录页面</h1></div >"
    26. }
    27. router = new VueRouter({
    28. routes: [
    29. // 请求的是根路径就重定向#/login登陆页面
    30. { path: '/', redirect: "/login" },
    31. { path: '/login', component: login },
    32. { path: '/register', component: register }
    33. ]
    34. // routes // (缩写) 相当于 routes: routes
    35. })
    36. var vm = new Vue({
    37. el: '#app',
    38. data: {},
    39. methods: {},
    40. router: router
    41. });
    42. </script>
    43. </body>
    44. </html>

    2.gif