vue动态组件is

亦凉 2023-02-28 15:15 28阅读 0赞

在一个多标签的界面中使用 is attribute 来切换不同的组件
compOne.vue

  1. <template>
  2. <div>组件1</div>
  3. </template>

compTwo.vue

  1. <template>
  2. <div>组件2</div>
  3. </template>

exchange.vue

  1. <template>
  2. <div>
  3. <el-button @click.native="changeComponent">切换</el-button>
  4. <component :is="currentTabComponent"></component>
  5. </div>
  6. </template>
  7. <script>
  8. import compOne from './compOne'
  9. import compTwo from './compTwo'
  10. export default {
  11. name: 'exchange',
  12. data() {
  13. return {
  14. currentTabComponent: 'compOne'
  15. }
  16. },
  17. components: {
  18. compOne,
  19. compTwo,
  20. },
  21. methods: {
  22. changeComponent() {
  23. if (this.currentTabComponent === 'compOne') {
  24. this.currentTabComponent = 'compTwo'
  25. } else {
  26. this.currentTabComponent = 'compOne'
  27. }
  28. }
  29. }
  30. }
  31. </script>

在这里插入图片描述

发表评论

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

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

相关阅读

    相关 Vue 动态组件

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

    相关 Vue动态组件

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