可以用v-on指令监听 DOM 事件,并在触发时运行一些 JavaScript 代码。

    DOM是文档对象模型, HTML DOM允许JavaScript对HTML事件作出反应

    v-on: 简写 @

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>Title</title>
    6. <script src="https://cdn.jsdelivr.net/npm/vue"></script>
    7. </head>
    8. <body>
    9. <div id="app">
    10. <span>{{message}}</span>
    11. <button v-on:click="login">登录</button>
    12. <a href="javascript:;" @click="register">注册</a>
    13. <button @click="add(counter)">点击+1</button>
    14. </div>
    15. </body>
    16. <script>
    17. var vm = new Vue({
    18. el: "#app",
    19. data: {
    20. message: "hello",
    21. counter: 1,
    22. total: 0,
    23. },
    24. methods: {
    25. login: function () {
    26. alert("我被点击了");
    27. },
    28. register: function () {
    29. alert("注册");
    30. },
    31. add: function (counter) {
    32. this.total += counter;
    33. alert(this.total);
    34. }
    35. }
    36. })
    37. </script>
    38. </html>