19_components_模版.html 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. <template id="box-temp">
  29. <div class="container">
  30. <div @click="box1Handle" class="box">
  31. <span>hello world</span>
  32. </div>
  33. <div @click="box2Handle" class="box2">
  34. {{num}}
  35. </div>
  36. </div>
  37. </template>
  38. <script>
  39. let app = new Vue({
  40. el: '#app',
  41. data: {
  42. },
  43. methods: {
  44. appHandle() {
  45. console.log('appHandle')
  46. }
  47. },
  48. components: {
  49. box: {
  50. template: "#box-temp",
  51. // 组件内部的data是个函数 内部返回一个对象
  52. data() {
  53. return{
  54. num:"abcd"
  55. }
  56. },
  57. methods: {
  58. box1Handle() {
  59. console.log('box1Handle')
  60. },
  61. box2Handle(){
  62. console.log('box2Handle')
  63. }
  64. }
  65. }
  66. }
  67. })
  68. </script>
  69. </body>
  70. </html>