Blogger Information
Blog 67
fans 0
comment 2
visits 72025
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
VUE多层组件传递,中间层传递的坑
搁浅
Original
486 people have browsed it

父组件cu.vue

  1. <template>
  2. <div>
  3. <div class="father" @click="father">我是父组件,点击传值给孙子组件</div>
  4. <child :title="title" :type="type" />
  5. </div>
  6. </template>
  7. <script>
  8. import child from "./child";
  9. export default {
  10. name: "cu",
  11. components: {
  12. child
  13. },
  14. data() {
  15. return {
  16. title: "2",
  17. type: "1"
  18. };
  19. },
  20. methods: {
  21. father() {
  22. this.title = "========> 父组件传过来的title";
  23. this.type = "========> 父组件传过来的type";
  24. }
  25. }
  26. };
  27. </script>

儿子组件child.vue

  1. <template>
  2. <div>
  3. <div class="child">
  4. <div>我是儿子组件{{child_title}}</div>
  5. </div>
  6. <grandson v-bind="$attrs" />
  7. </div>
  8. </template>
  9. <script>
  10. import grandson from "./grandson";
  11. export default {
  12. components: {
  13. grandson
  14. },
  15. props: {//如果这里用props接收了父组件传的值,那孙子组件就接收不到父组件的传值,注意是大坑。
  16. title: {
  17. type: String,
  18. default: ""
  19. }
  20. },
  21. watch: {
  22. $attrs() {
  23. console.log(this.$attrs, "attrs");
  24. },
  25. title() {
  26. this.child_title = this.title;
  27. console.log(this.title, "========》 子组件");
  28. console.log(this.$attrs, "attrs");
  29. }
  30. },
  31. data() {
  32. return {
  33. child_title: "",
  34. childType: ""
  35. };
  36. }
  37. };
  38. </script>

孙子组件child.vue

  1. <template>
  2. <div>
  3. <div class="grandson">我是孙子组件{{title}}{{type}}</div>
  4. </div>
  5. </template>
  6. <script>
  7. export default {
  8. props: {
  9. title: {
  10. type: String,
  11. default: ""
  12. },
  13. type: {
  14. type: String,
  15. default: ""
  16. }
  17. },
  18. watch: {
  19. title() {
  20. console.log(this.title, "this.title =====> 1孙子组件");
  21. },
  22. type() {
  23. console.log(this.type, "this.type =====> 2孙子组件");
  24. }
  25. },
  26. data() {
  27. return {};
  28. }
  29. };
  30. </script>
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
Author's latest blog post