Blogger Information
Blog 47
fans 3
comment 0
visits 38121
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Vue实例演示父子组件之间通信方式
Original
515 people have browsed it

1.父组件向子组件通过自定义参数传参

  1. <div id="app">
  2. <btn-inc :my-name="username" :my-count="count"></btn-inc>
  3. </div>
  4. <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  5. <script>
  6. const vm = new Vue({
  7. el: '#app',
  8. data() {
  9. return {
  10. username: '天蓬大人',
  11. count: 0,
  12. }
  13. },
  14. // 局部组件
  15. components: {
  16. btnInc: {
  17. props: ["myName", "myCount"],
  18. template: `
  19. <div>
  20. <button @click="num++">点赞:+ {{num}}</button>
  21. <span>{{myName}}</span>
  22. </div>
  23. `,
  24. data() {
  25. return {
  26. num: this.myCount,
  27. };
  28. }
  29. }
  30. }
  31. })
  32. </script>

2.子组件向父组件通过自定义方法传参

  1. <div id="app">
  2. <btn-inc :my-name="username" :my-count="count" @click-count="handle"></btn-inc>
  3. </div>
  4. <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  5. <script>
  6. const vm = new Vue({
  7. el: '#app',
  8. data() {
  9. return {
  10. username: '天蓬老师',
  11. count: 0,
  12. };
  13. },
  14. // 局部组件
  15. components: {
  16. btnInc: {
  17. props: ["myName", "myCount"],
  18. template: `
  19. <div>
  20. <button @click="$emit('click-count',10)">点赞:+ {{myCount}}</button>
  21. <span>{{myName}}</span>
  22. </div>
  23. `,
  24. },
  25. },
  26. // 子组件更新父组件的自定义方法
  27. methods: {
  28. handle(value) {
  29. console.log(this.count);
  30. this.count += value;
  31. this.username = "天蓬大人";
  32. }
  33. },
  34. })
  35. </script>
Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments