13_vue组件事件.html 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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. </head>
  8. <body>
  9. <div id="app">
  10. <h1 @click="clickFun">这是一个vue实例的h1标题</h1>
  11. <box></box>
  12. </div>
  13. <template id="box-temp">
  14. <div>
  15. <h1 @click="clickHandle">这是一个组件的h1标题</h1>
  16. </div>
  17. </template>
  18. <script src="./js/vue.js"></script>
  19. <script>
  20. new Vue({
  21. el: '#app',
  22. methods:{
  23. clickFun(){
  24. console.log("父组件h1被点击了");
  25. }
  26. },
  27. components:{
  28. "box":{
  29. template:"#box-temp",
  30. methods:{
  31. clickHandle(){
  32. console.log("子组件h1被点击了");
  33. }
  34. }
  35. }
  36. }
  37. })
  38. </script>
  39. </body>
  40. </html>