emit事件传递.html 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <!DOCTYPE html>
  2. <html lang="zh">
  3. <head>
  4. <title></title>
  5. <meta charset="UTF-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1">
  7. <script src="https://unpkg.com/vue/dist/vue.js"></script>
  8. <link href="../css/style.css" rel="stylesheet"> </head>
  9. <body>
  10. <div id="myApp">
  11. <h1>人生加法</h1>
  12. <add-method :a="6" :b="12" v-on:add_event="getAddResult"></add-method>
  13. <hr/>
  14. <h3>{{result}}</h3>
  15. </div>
  16. <script>
  17. Vue.component('add-method', {
  18. props: ['a', 'b'],
  19. template: '<div><button v-on:click="add">加吧</button></div>',
  20. methods: {
  21. add: function(){
  22. var value = 0;
  23. value = this.a + this.b;
  24. this.$emit('add_event', {
  25. result:value
  26. });
  27. }
  28. },
  29. });
  30. var myApp = new Vue({
  31. el: '#myApp',
  32. data: {
  33. result: 0
  34. },
  35. methods: {
  36. getAddResult: function(pval){
  37. this.result = pval.result;
  38. }
  39. },
  40. });
  41. </script>
  42. </body>
  43. </html>