20_components_多组件.html 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. <list-content></list-content>
  26. <box></box>
  27. <button @click="appHandle">button</button>
  28. </div>
  29. <template id="box-temp">
  30. <div class="container">
  31. <div @click="box1Handle" class="box">
  32. <span>hello world</span>
  33. </div>
  34. <div @click="box2Handle" class="box2">
  35. {{num}}
  36. </div>
  37. </div>
  38. </template>
  39. <template id="list-content">
  40. <div class="container">
  41. <ul>
  42. <li>1</li>
  43. <li>2</li>
  44. <li>3</li>
  45. <li>4</li>
  46. <li>5</li>
  47. </ul>
  48. </div>
  49. </template>
  50. <script>
  51. let app = new Vue({
  52. el: '#app',
  53. data: {
  54. },
  55. methods: {
  56. appHandle() {
  57. console.log('appHandle')
  58. }
  59. },
  60. components: {
  61. listContent:{
  62. template:"#list-content",
  63. },
  64. box: {
  65. template: "#box-temp",
  66. // 组件内部的data是个函数 内部返回一个对象
  67. data() {
  68. return{
  69. num:"abcd"
  70. }
  71. },
  72. methods: {
  73. box1Handle() {
  74. console.log('box1Handle')
  75. },
  76. box2Handle(){
  77. console.log('box2Handle')
  78. }
  79. }
  80. }
  81. }
  82. })
  83. </script>
  84. </body>
  85. </html>