App.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <template>
  2. <div class="app">
  3. <h1>组件通信</h1>
  4. <Demo1 ref="getMain"></Demo1>
  5. <hr />
  6. <Demo2 :name="a" sex="女" :age="10"></Demo2>
  7. <hr />
  8. <Demo3 @getName="getName"></Demo3>
  9. <hr />
  10. <Demo4></Demo4>
  11. <hr />
  12. <Demo5></Demo5>
  13. <hr />
  14. <Demo6></Demo6>
  15. <hr />
  16. <Demo7></Demo7>
  17. <hr />
  18. <Demo8>
  19. <!-- 默认插槽 -->
  20. <slot></slot>
  21. </Demo8>
  22. <hr />
  23. <Demo9>
  24. <template v-slot:header>
  25. <slot>哈哈哈</slot>
  26. </template>
  27. <template #footer>
  28. <slot>哒哒哒</slot>
  29. </template>
  30. <template #default>
  31. <slot>呵呵呵</slot>
  32. </template>
  33. </Demo9>
  34. <hr />
  35. <Demo10>
  36. <template v-slot="data">
  37. <slot>{{ data.obj.description }}</slot>
  38. </template>
  39. </Demo10>
  40. <hr />
  41. <Demo11 title="综合演示">
  42. <template v-slot:main="{holiday1}" >
  43. <slot>
  44. <div v-for="(item,index) in holiday1" :key="index">
  45. {{item}}
  46. </div>
  47. </slot>
  48. </template>
  49. <template #default>
  50. <slot>就不告诉你</slot>
  51. </template>
  52. </Demo11>
  53. <hr>
  54. <Demo12></Demo12>
  55. </div>
  56. </template>
  57. <script>
  58. import Demo1 from "./components/Demo1.vue";
  59. import Demo2 from "./components/Demo2.vue";
  60. import Demo3 from "./components/Demo3.vue";
  61. import Demo4 from "./components/Demo4.vue";
  62. import Demo5 from "./components/Demo5.vue";
  63. import Demo6 from "./components/Demo6.vue";
  64. import Demo7 from "./components/Demo7.vue";
  65. import Demo8 from "./components/Demo8.vue";
  66. import Demo9 from "./components/Demo9.vue";
  67. import Demo10 from "./components/Demo10.vue";
  68. import Demo11 from "./components/Demo11.vue";
  69. import Demo12 from "./components/Demo12.vue";
  70. export default {
  71. data() {
  72. return {
  73. msg: "哈哈哈哈",
  74. a: "小李",
  75. };
  76. },
  77. components: {
  78. Demo1,
  79. Demo2,
  80. Demo3,
  81. Demo4,
  82. Demo5,
  83. Demo6,
  84. Demo7,
  85. Demo8,
  86. Demo9,
  87. Demo10,
  88. Demo11,
  89. Demo12
  90. },
  91. methods: {
  92. ab() {
  93. console.log("明天休息", this.msg);
  94. },
  95. getName(name) {
  96. console.log("我叫" + name);
  97. },
  98. },
  99. mounted() {
  100. /**
  101. * on 绑定上
  102. * emit 监听
  103. * off 解绑
  104. * */
  105. this.$refs.getMain.$on("aa", this.ab);
  106. },
  107. destroyed() {
  108. this.$refs.getMain.$off("aa", this.ab);
  109. },
  110. };
  111. </script>
  112. <style scoped></style>