17_components绑定事件.html 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. <title>Document</title>
  7. <script src="./js/vue.js"></script>
  8. <style>
  9. .box {
  10. width: 100px;
  11. height: 100px;
  12. background-color: red;
  13. margin: 10px;
  14. }
  15. .box2 {
  16. width: 100px;
  17. height: 100px;
  18. background-color: blue;
  19. margin: 10px;
  20. }
  21. </style>
  22. </head>
  23. <body>
  24. <div id="app">
  25. <box></box>
  26. <button @click="appHandle">button</button>
  27. </div>
  28. <script>
  29. let app = new Vue({
  30. el: '#app',
  31. data: {
  32. },
  33. methods: {
  34. appHandle() {
  35. console.log('appHandle')
  36. }
  37. },
  38. components: {
  39. box: {
  40. template: `
  41. <div class="container">
  42. <div @click="box1Handle" class="box">
  43. <span>hello world</span>
  44. </div>
  45. <div @click="box2Handle" class="box2"></div>
  46. </div>
  47. `,
  48. methods: {
  49. box1Handle() {
  50. console.log('box1Handle')
  51. },
  52. box2Handle(){
  53. console.log('box2Handle')
  54. }
  55. }
  56. }
  57. }
  58. })
  59. </script>
  60. </body>
  61. </html>