加入收藏 | 设为首页 | 会员中心 | 我要投稿 汽车网 (https://www.0577qiche.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 教程 > 正文

Vue动态组件如何使用

发布时间:2023-04-18 15:10:09 所属栏目:教程 来源:
导读:动态组件是让多个组件使用同一个挂载点,并动态切换。动态组件是 Vue 的一个高级用法,但其实它的使用非常简单。keep-alive 是 vue 的内置组件,能在组件切换过程中将状态保存在内存中,防止重复渲染 DOM。

动态组
动态组件是让多个组件使用同一个挂载点,并动态切换。动态组件是 Vue 的一个高级用法,但其实它的使用非常简单。keep-alive 是 vue 的内置组件,能在组件切换过程中将状态保存在内存中,防止重复渲染 DOM。

动态组件如何使用
通过使用保留的 <component> 元素,动态地把组件名称绑定到它的 is 特性,可以实现动态组件:

<!DOCTYPE html>
<html lang="en">
<head>
  <Meta charset="UTF-8">
  <Meta name="viewport" content="width=device-width, initial-scale=1.0">
  <Meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <div id="app">
    <component :is="currentView"></component>
    <button @click="changeView('A')">切换到A</button>
    <button @click="changeView('B')">切换到B</button>
    <button @click="changeView('C')">切换到C</button>
  </div>
</body>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script type="text/javascript">
  Vue.component('ComponentA', {
    template: '<div> 组件 A </div>',
  })
  Vue.component('ComponentB', {
    template: '<div> 组件 B </div>',
  })
  Vue.component('ComponentC', {
    template: '<div> 组件 C </div>',
  })
  var vm = new Vue({
    el: '#app',
    data() {
        return {
        currentView: 'ComponentB'
      }
    },
    methods: {
      changeView(name) {
        this.currentView = `Component${name}`
      }
    }
  })
</script>
</html>
代码解释:
HTML 代码第 2 行,我们使用动态组件 component,将当前需要展示的组件名通过变量 currentView 绑定到 component 的 is 属性上。
HTML 代码第 3-5 行,我们定义了三个按钮,通过点击按钮切换 currentView 的值。
JS 代码第 3-11 行,我们定义了组件 ComponentA、ComponentB、ComponentC。

最终的实现效果是:当点击按钮的时候会动态切换展示的组件。

keep-alive
keep-alive 是 Vue 提供的一个抽象组件,用来对组件进行缓存,从而节省性能,由于是一个抽象组件,所以在页面渲染完毕后不会被渲染成一个 DOM 元素。被 keep-alive 缓存的组件只有在初次渲染时才会被创建,并且当组件切换时不会被销毁。

基础用法
keep-alive 的用法相对简单,直接使用 keep-alive 包裹需要缓存的组件即可:

<!DOCTYPE html>
<html lang="en">
<head>
  <Meta charset="UTF-8">
  <Meta name="viewport" content="width=device-width, initial-scale=1.0">
  <Meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <div id="app">
    <keep-alive>
      <component :is="currentView"></component>
    </keep-alive>
    <button @click="changeView('A')">切换到A</button>
    <button @click="changeView('B')">切换到B</button>
    <button @click="changeView('C')">切换到C</button>
  </div>
</body>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script type="text/javascript">
  Vue.component('ComponentA', {
    template: '<div> 组件 A </div>',
    created() {
      console.log('组件A created')
    }
  })
  Vue.component('ComponentB', {
    template: '<div> 组件 B </div>',
    created() {
      console.log('组件B created')
    }
  })
  Vue.component('ComponentC', {
    template: '<div> 组件 C </div>',
    created() {
      console.log('组件C created')
    }
  })
  var vm = new Vue({
    el: '#app',
    data() {
        return {
        currentView: 'ComponentB'
      }
    },
    methods: {
      changeView(name) {
        this.currentView = `Component${name}`
      }
    }
  })
</script>
</html>
代码解释:
HTML 代码第 2-3 行,我们使用 keep-alive 包裹动态组件 component,将当前需要展示的组件名通过变量 currentView 绑定到 component 的 is 属性上。
HTML 代码第 5-7 行,我们定义了三个按钮,通过点击按钮切换 currentView 的值。
JS 代码第 3-29 行,我们定义了组件 ComponentA、ComponentB、ComponentC,分别定义了他们的 created 和 beforeDestroy 事件。

(编辑:汽车网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章