VueJS 之动态组件

36 篇文章 2 订阅
16 篇文章 1 订阅
本文介绍了VueJS中如何实现动态组件的切换,包括基本的组件切换、利用keep-alive进行组件缓存以保持状态,以及include和exclude属性的选择性缓存。同时,讨论了组件在缓存过程中触发的activated和deactivated生命周期钩子。
摘要由CSDN通过智能技术生成

参考

项目描述
搜索引擎Bing
哔哩哔哩黑马程序员
VueJS 官方文档动态组件 & 异步组件

描述

项目描述
Edge109.0.1518.70 (正式版本) (64 位)
操作系统Windows 10 专业版
@vue/cli5.0.8
npm8.19.3
VueJS2.6.14

动态组件

动态组件即动态切换组件,实现组件的动态切换可以使用 VueJS 提供的 component 元素,该元素拥有一个 is 属性。
你可以向 component 元素提供一个组件的名称来作为 is 属性的参数,接下来对应的组件将替代 components 元素。

子组件

在该部分内容中需要用到两个子组件,即 First.vueLast.vue。接下来的内容中,如果没有特殊提示,First.vueLast.vue 中的内容均如下所示。

First.vue

<template>
  <div class="container"></div>
</template>

<script>
export default {

}
</script>

<style scoped>
.container{
    width: 400px;
    height: 180px;
    background-color: dodgerblue;
}
</style>

Last.vue

<template>
  <div class="container"></div>
</template>

<script>
export default {

}
</script>

<style scoped>
.container{
    width: 400px;
    height: 180px;
    background-color: pink;
}
</style>

举个栗子

展示组件 First.vue

<template>
  <div class="container">
    <component is="First"></component>
  </div>
</template>

<script>
// 导入组件
import First from '@/components/First.vue';
import Last from '@/components/Last.vue';

export default {
  // 注册组件
  components: {
    First, 
    Last
  }
}
</script>

<style>

</style>

执行结果:

效果

展示组件 Last.vue

<template>
  <div class="container">
    <component is="Last"></component>
  </div>
</template>

<script>
// 导入组件
import First from '@/components/First.vue';
import Last from '@/components/Last.vue';

export default {
  // 注册组件
  components: {
    First, 
    Last
  }
}
</script>

<style>

</style>

执行结果:

效果

切换组件

我们可以定义一个数据并将该数据作为 component 元素的 is 属性值,这个数据保存了组件的名称,当我们需要切换组件时,只需要改变数据所保存的内容即可。

举个栗子

<template>
  <div class="container">
    <!-- 使用属性绑定指令将指定的数据绑定到属性中 -->
    <component v-bind:is="show"></component>
    <button @click="transfer()">Transfer</button>
  </div>
</template>

<script>
// 导入组件
import First from '@/components/First.vue';
import Last from '@/components/Last.vue';

export default {
  // 定义数据
  data() {
    return {
      show: 'First'
    }
  },
  // 注册组件
  components: {
    First, 
    Last
  },
  // 定义事件处理函数
  methods: {
    transfer() {
      if(this.show === 'First'){
        this.show = 'Last';
      }else{
        this.show = 'First';
      }
    }
  }
}
</script>

<style>

</style>

执行效果:

效果

keep-alive

子组件

在该部分内容中需要用到两个子组件,即 First.vueLast.vue。接下来的内容中,如果没有特殊提示,First.vueLast.vue 中的内容均如下所示。

子组件 First.vue

<template>
  <div class="container">
    <h3>Number: {{ num }}</h3>
    <button @click="add()">Add</button>
  </div>
</template>

<script>
export default {
    // 定义数据
    data() {
        return {
            num: 0
        }
    },
    // 定义事件调用函数
    methods: {
        add() {
            this.num += 1;
        }
    }
}
</script>

<style scoped>
.container{
    width: 400px;
    height: 180px;
    background-color: dodgerblue;
}
h3{
    color: #fff;
}
</style>

子组件 Last.vue

<template>
  <div class="container"></div>
</template>

<script>
export default {

}
</script>

<style scoped>
.container{
    width: 400px;
    height: 180px;
    background-color: pink;
}
</style>

组件的再生成

在组件的动态切换过程中,存在组件的生成与销毁过程。

<template>
  <div class="container">
    <!-- 使用属性绑定指令将指定的数据绑定到属性中 -->
    <component v-bind:is="show"></component>
    <button @click="transfer()">Transfer</button>
  </div>
</template>

<script>
// 导入组件
import First from '@/components/First.vue';
import Last from '@/components/Last.vue';

export default {
  // 定义数据
  data() {
    return {
      show: 'First'
    }
  },
  // 注册组件
  components: {
    First, 
    Last
  },
  // 定义事件处理函数
  methods: {
    transfer() {
      if(this.show === 'First'){
        this.show = 'Last';
      }else{
        this.show = 'First';
      }
    }
  }
}
</script>

<style>

</style>

执行效果:

效果

分析:

在切换组件后,原组件将被销毁。再次切换为原组件时,由于组件经历了再生成的过程,组件中的数据都将处于初始状态。

keep-alive

在实现组件的动态切换功能时,你可能会想保持这些组件的状态,以避免反复重新渲染导致的性能问题。我们可以使用 keep-alive 元素达成该目的。

keep-alive 能够使被替换的组件不用经历组件的再生成过程,被替换的组件将处于缓存状态,等待用户的再次使用。keep-alive 的使用方式很简单,你仅需要使用 keep-alive 元素将需要被缓存的动态组件包裹起来。

举个栗子

<template>
  <div class="container">
    <keep-alive>
      <!-- 使用属性绑定指令将指定的数据绑定到属性中 -->
      <component v-bind:is="show"></component>
    </keep-alive>
    <button @click="transfer()">Transfer</button>
  </div>
</template>

<script>
// 导入组件
import First from '@/components/First.vue';
import Last from '@/components/Last.vue';

export default {
  // 定义数据
  data() {
    return {
      show: 'First'
    }
  },
  // 注册组件
  components: {
    First, 
    Last
  },
  // 定义事件处理函数
  methods: {
    transfer() {
      if(this.show === 'First'){
        this.show = 'Last';
      }else{
        this.show = 'First';
      }
    }
  }
}
</script>

<style>

</style>

执行效果:

效果

include

keep-alive 元素拥有 include 属性,你可以通过向该属性提交组件名来指定需要被缓存的组件。如果有多个组件需要被缓存,请使用逗号分隔组件名。

举个栗子

<template>
  <div class="container">
    <!-- 指定 First 组件需要被缓存 -->
    <keep-alive  include="First">
      <!-- 使用属性绑定指令将指定的数据绑定到属性中 -->
      <component v-bind:is="show"></component>
    </keep-alive>
    <button @click="transfer()">Transfer</button>
  </div>
</template>

<script>
// 导入组件
import First from '@/components/First.vue';
import Last from '@/components/Last.vue';

export default {
  // 定义数据
  data() {
    return {
      show: 'First'
    }
  },
  // 注册组件
  components: {
    First, 
    Last
  },
  // 定义事件处理函数
  methods: {
    transfer() {
      if(this.show === 'First'){
        this.show = 'Last';
      }else{
        this.show = 'First';
      }
    }
  }
}
</script>

<style>

</style>

执行效果

执行效果

exclude

keep-alive 元素拥有 exclude 属性,你可以通过向该属性提交组件名来指定不需要被缓存的组件。如果有多个组件不需要被缓存,请使用逗号分隔组件名。

举个栗子

<template>
  <div class="container">
    <!-- 指定 First 组件不需要被缓存 -->
    <keep-alive  exclude="First">
      <!-- 使用属性绑定指令将指定的数据绑定到属性中 -->
      <component v-bind:is="show"></component>
    </keep-alive>
    <button @click="transfer()">Transfer</button>
  </div>
</template>

<script>
// 导入组件
import First from '@/components/First.vue';
import Last from '@/components/Last.vue';

export default {
  // 定义数据
  data() {
    return {
      show: 'First'
    }
  },
  // 注册组件
  components: {
    First, 
    Last
  },
  // 定义事件处理函数
  methods: {
    transfer() {
      if(this.show === 'First'){
        this.show = 'Last';
      }else{
        this.show = 'First';
      }
    }
  }
}
</script>

<style>

</style>

执行效果

效果

生命周期钩子

在组件被缓存或被激活的时候将触发相应的生命周期钩子(生命周期函数)。

  1. 当组件被激活时将触发 activated 生命周期钩子。
  2. 当组件被缓存时将触发 deactivated 生命周期钩子。

举个栗子

<template>
  <div class="container">
    <keep-alive>
      <!-- 使用属性绑定指令将指定的数据绑定到属性中 -->
      <component v-bind:is="show"></component>
    </keep-alive>
    <button @click="transfer()">Transfer</button>
  </div>
</template>

<script>
// 导入组件
import First from '@/components/First.vue';
import Last from '@/components/Last.vue';

export default {
  // 定义数据
  data() {
    return {
      show: 'First'
    }
  },
  // 注册组件
  components: {
    First, 
    Last
  },
  // 定义事件处理函数
  methods: {
    transfer() {
      if(this.show === 'First'){
        this.show = 'Last';
      }else{
        this.show = 'First';
      }
    }
  }
}
</script>

<style>

</style>

请将子组件 First.vue 修改为:

<template>
  <div class="container">
    <h3>Number: {{ num }}</h3>
    <button @click="add()">Add</button>
  </div>
</template>

<script>
export default {
    // 定义数据
    data() {
        return {
            num: 0
        }
    },
    // 定义事件调用函数
    methods: {
        add() {
            this.num += 1;
        }
    },
    // activated 生命周期对应的生命周期钩子
    activated() {
        console.log('【Activated】')
    },
    // deactivated 生命周期对应的生命周期钩子
    deactivated() {
        console.log('【Deactivated】')
    }
}
</script>

<style scoped>
.container{
    width: 400px;
    height: 180px;
    background-color: dodgerblue;
}
h3{
    color: #fff;
}
</style>

执行效果:

效果

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BinaryMoon

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值