• Vue:(五)事件初探


    基本使用

    1. 使用v-on:xxx 或 @xxx 绑定事件,其中xxx为事件名
    2. 事件的回调需要配置在methods对象中,最终会在vm上
    3. methods中配置的对象,不能用箭头函数,否则this就不是vm了
    4. methods中配置的函数,都是被Vue所管理的函数,this的指向是vm 或者 组件实例对象
    5. @click="demo" 和 @clink="demo(event)" 效果一致,但后者可以传参
    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8" />
    5. <title>事件的基本处理title>
    6. <script type="text/javascript" src="../js/vue.js">script>
    7. head>
    8. <body>
    9. <div id="root">
    10. <h2>欢迎来到{{name}}h2>
    11. <button @click="show($event,66)">请点击我button>
    12. div>
    13. <script type="text/javascript">
    14. const vm = new Vue({
    15. el: '#root',
    16. data: {
    17. name: '空谷有来人',
    18. },
    19. methods: {
    20. show(event, number) {
    21. console.log(number)
    22. alert('欢迎你')
    23. },
    24. },
    25. })
    26. script>
    27. body>
    28. html>

    修饰符

    1. prevent:阻止默认事件(常用)
    2. stop:阻止事件冒泡(常用)
    3. once、:事件只触发一次(常用)
    4. capture:使用事件的捕获模式
    5. self:只有event.target是当前操作的元素时才触发事件
    6. passive:事件的默认行为立即执行,无需等待事件回调执行完毕
    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8" />
    5. <title>事件修饰符title>
    6. <script type="text/javascript" src="../js/vue.js">script>
    7. <style>
    8. * {
    9. margin-top: 20px;
    10. }
    11. .demo1 {
    12. height: 50px;
    13. background-color: aquamarine;
    14. }
    15. .box1 {
    16. padding: 5px;
    17. background-color: aqua;
    18. }
    19. .box2 {
    20. background-color: orange;
    21. }
    22. style>
    23. head>
    24. <body>
    25. <div id="root">
    26. <h2>欢迎来到{{name}}h2>
    27. <a href="https://www.baidu.com" @click.prevent="showinfo">点击我a>
    28. <div class="demo1" @click="showinfo">
    29. <button @click.stop="showinfo">点击我button>
    30. div>
    31. <button @click.once="showinfo">点击我button>
    32. <div class="box1" @click.capture="showMsg(1)">
    33. div1
    34. <div class="box2" @click="showMsg(2)">div2div>
    35. div>
    36. <div class="demo1" @click.self="showinfo">
    37. <button @click="showinfo">点击我button>
    38. div>
    39. div>
    40. <script type="text/javascript">
    41. new Vue({
    42. el: '#root',
    43. data: {
    44. name: '空谷有来人',
    45. },
    46. methods: {
    47. showinfo(e) {
    48. // e.preventDefault()
    49. // e.stopPropagation()
    50. // console.log(e.target)
    51. alert('欢迎')
    52. },
    53. showMsg(num) {
    54. console.log(num)
    55. },
    56. },
    57. })
    58. script>
    59. body>
    60. html>

    我是空谷有来人,感谢大家支持!

  • 相关阅读:
    STM32CubeMX ADC参数配置页中文注解
    VFP技巧
    自学Python01-创建文件写入内容
    Web3:价值投资的范式转移
    配置Webpack Dev Server 实战操作方法步骤
    多参加活动,生活才精彩
    Sentinel注解@SentinelResource详解
    随机场的生成 random field generation-考虑相关长度-GSTOOL
    iperf3交叉编译
    MyBatis-Plus中实现自定义功能Mapper
  • 原文地址:https://blog.csdn.net/weixin_41377877/article/details/127925837