18_components_data.html 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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">
  46. {{num}}
  47. </div>
  48. </div>
  49. `,
  50. data() {
  51. return{
  52. num:"abcd"
  53. }
  54. },
  55. methods: {
  56. box1Handle() {
  57. console.log('box1Handle')
  58. },
  59. box2Handle(){
  60. console.log('box2Handle')
  61. }
  62. }
  63. }
  64. }
  65. })
  66. </script>
  67. </body>
  68. </html>