【Vue】—动态组件

迈不过友情╰ 2022-09-11 12:22 305阅读 0赞

【Vue】—动态组件

在这里插入图片描述

  1. <template>
  2. <div>
  3. <div>这里使用动态组件包装</div>
  4. <!-- 能显示不同的组件 -->
  5. <div>
  6. <keep-alive exclude="A">
  7. <component :is='curComp' />
  8. </keep-alive>
  9. <button @click="toggle">切换</button>
  10. </div>
  11. <div>
  12. <component :is='ccdd' />
  13. <button @click="cad">切换c和d</button>
  14. </div>
  15. </div>
  16. </template>
  17. <script>
  18. import A from './a';
  19. import B from './b';
  20. import C from './c';
  21. import D from './d';
  22. export default {
  23. components: {
  24. A,
  25. B,
  26. C,
  27. D
  28. },
  29. data() {
  30. return {
  31. curComp: A,
  32. ccdd: C
  33. };
  34. },
  35. methods: {
  36. toggle() {
  37. this.curComp = this.curComp == A ? B : A;
  38. },
  39. cad() {
  40. this.ccdd = this.ccdd == C ? D : C
  41. }
  42. }
  43. }
  44. </script>
  45. <style>
  46. </style>

发表评论

表情:
评论列表 (有 0 条评论,305人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Vue 动态组件

    动态组件 通过使用保留的 <component> 元素,并对其 is 特性进行动态绑定,你可以在同一个挂载点动态切换多个组件: var vm = new Vue

    相关 Vue动态组件

    Vue动态组件 1、序言 2、实例 1、序言   在页面应用程序中,经常会遇到多标签页面,在Vue.js中,可以通过动态组件来实现。组件的动态切换是通