index.html 924 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <script src="../vue.js"></script>
  7. </head>
  8. <body>
  9. <div id="app">
  10. <counter :count="3" @change="handleCount"></counter>
  11. <counter :count="2" @change="handleCount"></counter>
  12. <div>{{total}}</div>
  13. </div>
  14. <script>
  15. var counter = {
  16. props: ["count"],
  17. data: function (){
  18. return {
  19. number: this.count
  20. }
  21. },
  22. template: '<div @click="handleClick">{{number}}</div>',
  23. methods: {
  24. handleClick: function () {
  25. this.number ++;
  26. this.$emit('change',1)
  27. }
  28. }
  29. }
  30. var vm = new Vue({
  31. el: "#app",
  32. data: {
  33. total: 5
  34. },
  35. components: {
  36. counter: counter
  37. },
  38. methods: {
  39. handleCount: function (step){
  40. this.total += step
  41. }
  42. }
  43. })
  44. </script>
  45. </body>
  46. </html>