动态组件

收藏905

阅读24189

更新时间2025-08-22

<KeepAlive>

运行下面的示例。 您会注意到,当您切换回某个组件时,您在一个组件中所做的更改会被忘记。 这是因为组件被卸载并再次安装,从而重新加载该组件。

示例

此示例与前面的示例相同,只是组件不同。 在 comp-one 中,您可以在"Apple"和"Cake"之间进行选择,在 comp-two 中,您可以编写一条消息。 当您返回组件时,您的输入将会消失。

运行示例 »

为了保持状态、您之前的输入,当返回组件时,我们在 <component> 标签周围使用 <KeepAlive> 标签。

示例

The components now remember the user inputs.

App.vue:

<template>
  <h1>Dynamic Components</h1>
  <p>App.vue switches between which component to show.</p>
  <button @click="toggleValue = !toggleValue">
    Switch component
  </button>
  <KeepAlive>
    <component :is="activeComp"></component>
  </KeepAlive>
</template>
运行示例 »

'include' 和 'exclude' 属性

默认情况下,<KeepAlive> 标签内的所有组件都会保持活动状态。

但是我们也可以通过在 <KeepAlive> 标签上使用"include"或"exclude"属性来仅定义一些要保持活动的组件。

如果我们在 <KeepAlive> 标签上使用"include"或"exclude"属性,我们还需要使用"name"选项为组件提供名称:

CompOne.vue:

<script>
  export default {
    name: 'CompOne',
    data() {
      return {
        imgSrc: 'img_question.svg'
      }
    }
  }
</script>

示例

对于 <KeepAlive include="CompOne">,只有"CompOne"组件会记住其状态以及之前的输入。

App.vue:

<template>
  <h1>Dynamic Components</h1>
  <p>App.vue switches between which component to show.</p>
  <button @click="toggleValue = !toggleValue">
    Switch component
  </button>
  <KeepAlive include="CompOne">
    <component :is="activeComp"></component>
  </KeepAlive>
</template>
运行示例 »

我们还可以使用'exclude'来选择哪些组件保持活动状态或不保持活动状态。

示例

对于 <KeepAlive exclude="CompOne">,只有"CompTwo"组件会记住其状态。

App.vue:

<template>
  <h1>Dynamic Components</h1>
  <p>App.vue switches between which component to show.</p>
  <button @click="toggleValue = !toggleValue">
    Switch component
  </button>
  <KeepAlive exclude="CompOne">
    <component :is="activeComp"></component>
  </KeepAlive>
</template>
运行示例 »

通过使用逗号分隔,'include' 和 'exclude' 都可以与多个组件一起使用。

为了展示这一点,我们将再添加一个组件,以便总共获得三个组件。

示例

使用 <KeepAlive include="CompOne, CompThree">,"CompOne"和"CompThree"组件都会记住它们的状态。

App.vue:

<template>
  <h1>Dynamic Components</h1>
  <button @click="compNbr++">
    Next component
  </button>
  <KeepAlive include="CompOne,CompThree">
    <component :is="activeComp"></component>
  </KeepAlive>
</template>
运行示例 »

'max' 属性

我们可以使用"max"作为 <KeepAlive> 标签的属性来限制浏览器需要记住其状态的组件数量。

示例

使用 <KeepAlive :max="2">,浏览器将只记住最后两个访问组件的用户输入。

App.vue:

<template>
  <h1>Dynamic Components</h1>
  <label><input type="radio" name="rbgComp" v-model="compName" :value="'comp-one'"> One</label>
  <label><input type="radio" name="rbgComp" v-model="compName" :value="'comp-two'"> Two</label>
  <label><input type="radio" name="rbgComp" v-model="compName" :value="'comp-three'"> Three</label>
  <KeepAlive :max="2">
    <component :is="activeComp"></component>
  </KeepAlive>
</template>
运行示例 »

Vue 练习

通过练习测试自己

练习题:

制作动态组件时使用什么属性?

<component :="activeComp"></component>

相关

视频

RELATED VIDEOS

更多

免费

Web前端开发极速入门
初级 Web前端开发极速入门

220622次学习

收藏

免费

前端入门_HTML5
初级 前端入门_HTML5

619062次学习

收藏

免费

30分钟学会网站布局
初级 30分钟学会网站布局

239149次学习

收藏

免费

CSS视频教程-玉女心经版
初级 CSS视频教程-玉女心经版

394761次学习

收藏

免费

独孤九贱(1)_HTML5视频教程

免费

独孤九贱(6)_jQuery视频教程

免费

独孤九贱(7)_Bootstrap视频教程

免费

独孤九贱(2)_CSS视频教程
初级 独孤九贱(2)_CSS视频教程

230705次学习

收藏

科技资讯

更多

精选课程

更多
前端入门_HTML5
前端入门_HTML5

共29课时

62万人学习

CSS视频教程-玉女心经版
CSS视频教程-玉女心经版

共25课时

39.5万人学习

JavaScript极速入门_玉女心经系列
JavaScript极速入门_玉女心经系列

共43课时

71.2万人学习

独孤九贱(1)_HTML5视频教程
独孤九贱(1)_HTML5视频教程

共25课时

61.9万人学习

独孤九贱(2)_CSS视频教程
独孤九贱(2)_CSS视频教程

共22课时

23.1万人学习

独孤九贱(3)_JavaScript视频教程
独孤九贱(3)_JavaScript视频教程

共28课时

34.1万人学习

独孤九贱(4)_PHP视频教程
独孤九贱(4)_PHP视频教程

共89课时

125.7万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号