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. </head>
    10. <body>
    11. <template id="tmplogin">
    12. <h1>登陆组件</h1>
    13. </template>
    14. <template id="tmpregister">
    15. <h1>注册组件</h1>
    16. </template>
    17. <div id="app">
    18. <a href="" @click.prevent="login">登陆</a>
    19. <a href="" @click.prevent="register">注册</a>
    20. <component :is="chang"></component>
    21. </div>
    22. <script>
    23. Vue.component('login', {
    24. template: "#tmplogin"
    25. })
    26. Vue.component('register', {
    27. template: "#tmpregister"
    28. })
    29. var vm = new Vue({
    30. el: '#app',
    31. data: {
    32. chang: "login"
    33. },
    34. methods: {
    35. login() {
    36. this.chang = 'login'
    37. },
    38. register() {
    39. this.chang = 'register'
    40. }
    41. }
    42. });
    43. </script>
    44. </body>
    45. </html>

    1.gif