App.vue 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <template>
  2. <div>
  3. <Demo9></Demo9>
  4. <hr>
  5. <hr>
  6. <hr>
  7. <h1>第一个</h1>
  8. <!-- 3.使用组件 -->
  9. <Demo1 ref="getMain"></Demo1>
  10. <br />
  11. <Demo2 name="aa" :age="3" :dd="xxx"></Demo2>
  12. <hr />
  13. <Demo3 @getXX="getThing"></Demo3>
  14. <hr />
  15. <Demo4></Demo4>
  16. <hr />
  17. <hr />
  18. <Demo5 v-if="isShow"></Demo5>
  19. <button @click="isShow = !isShow">销毁demo5</button>
  20. <br />
  21. <hr />
  22. <Demo6>哈哈哈哈</Demo6>
  23. <Demo7>
  24. <template #main>
  25. <p>1111</p>
  26. </template>
  27. <template v-slot:header>
  28. <p>222</p>
  29. </template>
  30. <template #default>
  31. <p>333</p>
  32. </template>
  33. </Demo7>
  34. <Demo8>
  35. <template v-slot:x="data">
  36. <p>{{ data.obj1.name }}</p>
  37. </template>
  38. </Demo8>
  39. </div>
  40. </template>
  41. <script>
  42. // 1.组件的引入
  43. import Demo1 from "./components/Demo1.vue";
  44. import Demo2 from "./components/Demo2.vue";
  45. import Demo3 from "./components/Demo3.vue";
  46. import Demo4 from "./components/Demo4.vue";
  47. import Demo5 from "./components/Demo5.vue";
  48. import Demo6 from "./components/Demo6.vue";
  49. import Demo7 from "./components/Demo7.vue";
  50. import Demo8 from "./components/Demo8.vue";
  51. import Demo9 from "./components/Demo9.vue";
  52. export default {
  53. data() {
  54. return {
  55. xxx: "你好",
  56. isShow: true,
  57. };
  58. },
  59. // 2.组件的注册
  60. components: {
  61. Demo1,
  62. Demo2,
  63. Demo3,
  64. Demo4,
  65. Demo5,
  66. Demo6,
  67. Demo7,
  68. Demo8,
  69. Demo9
  70. },
  71. methods: {
  72. aa() {
  73. console.log("你好");
  74. },
  75. getThing(x) {
  76. console.log("触发事件" + x);
  77. },
  78. },
  79. mounted() {
  80. /**
  81. * 父组件:
  82. * 1.在想要获取的子组件上绑定ref='xxx'
  83. * 2.在挂载的生命周期上绑定事件
  84. * this.$refs.xxx.$on("事件名",事件)
  85. * 子组件:
  86. * 触发:this.$emit("事件名")
  87. * 解绑:this.$off("事件名")
  88. */
  89. this.$refs.getMain.$on("ab", this.aa);
  90. console.log(this.$refs.getMain.$on("ab", this.aa));
  91. // this.showMains();
  92. },
  93. };
  94. </script>
  95. <style>
  96. </style>