----------------------html、js、style-----------------------------------------------

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8"/>
        <link rel="stylesheet"  type="text/css" href="index.css"/>
        <script src="vue.js"></script>
    </head>
    <body>
        <div>
        <h1>--在动态组件上使用 keep-alive--</h1>
        <div id="example1" class="demo">
            <button
                class="dynamic-component-demo-tab-button"
                v-for="tab in tabs"
                v-bind:class="{ 'dynamic-component-demo-active' : tab === 'Posts' }"
                v-on:click="currentTab = tab"
            >
                {{ tab }}
            </button>
            <!--如果去掉keep-alive标签,在Posts和Archive直接切换时,将不会缓存另外一个-->
            <keep-alive>
                <component v-bind:is="currentTabComponent"></component>
            </keep-alive>
        </div>
        <script>
        Vue.component('tab-posts', {
            data: function () {
                return {
                    posts: [
                        {
                            id: 1,
                            title: 'Cat Ipsum',
                            content: '<p>Dont wait for the storm to pass, dance in the rain kick up litter decide to want nothing to do with my owner today demand to be let outside at once, and expect owner to wait for me as i think about it cat cat moo moo lick ears lick paws so make meme, make cute face but lick the other cats. Kitty poochy chase imaginary bugs, but stand in front of the computer screen. Sweet beast cat dog hate mouse eat string barf pillow no baths hate everything stare at guinea pigs. My left donut is missing, as is my right loved it, hated it, loved it, hated it scoot butt on the rug cat not kitten around.</p>'
                        },
                        {
                            id: 2,
                            title: 'Hipster Ipsum',
                            content: '<p>Bushwick blue bottle scenester helvetica ugh, meh four loko. Put a bird on it lumbersexual franzen shabby chic, street art knausgaard trust fund shaman scenester live-edge mixtape taxidermy viral yuccie succulents. Keytar poke bicycle rights, crucifix street art neutra air plant PBR&B hoodie plaid venmo. Tilde swag art party fanny pack vinyl letterpress venmo jean shorts offal mumblecore. Vice blog gentrify mlkshk tattooed occupy snackwave, hoodie craft beer next level migas 8-bit chartreuse. Trust fund food truck drinking vinegar gochujang.</p>'
                        },
                        {
                            id: 3,
                            title: 'Cupcake Ipsum',
                            content: '<p>Icing dessert soufflé lollipop chocolate bar sweet tart cake chupa chups. Soufflé marzipan jelly beans croissant toffee marzipan cupcake icing fruitcake. Muffin cake pudding soufflé wafer jelly bear claw sesame snaps marshmallow. Marzipan soufflé croissant lemon drops gingerbread sugar plum lemon drops apple pie gummies. Sweet roll donut oat cake toffee cake. Liquorice candy macaroon toffee cookie marzipan.</p>'
                        },
                    ],
                    selectedPost: null
                }
            },
            template: '\
                <div class="dynamic-component-demo-posts-tab">\
                    <ul class="dynamic-component-demo-posts-sidebar">\
                        <li\
                            v-for="post in posts"\
                            v-bind:key="post.id"\
                            v-bind:class="{ \'dynamic-component-demo-active\' : post === selectedPost }"\
                            v-on:click="selectedPost = post"\
                        >\
                            {{ post.title }}\
                        </li>\
                    </ul>\
                        <div class="dynamic-component-demo-post-container">\
                            <div\
                                v-if="selectedPost"\
                                class="dynamic-component-demo-post"\
                            >\
                                <h3>{{ selectedPost.title }}</h3>\
                                <div v-html="selectedPost.content"></div>\
                            </div>\
                            <strong v-else>\
                                Click on a blog title to the left to view it.\
                            </strong>\
                        </div>\
                </div>\
            '
        })
        Vue.component('tab-archive', {
            template: '<div>Archive component.</div>'
        })
        var example1 = new Vue({
            el:'#example1',
            data: {
                currentTab: 'Posts',
                tabs: ['Posts', 'Archive']
            },
            computed: {
                currentTabComponent: function () {
                    return 'tab-' + this.currentTab.toLowerCase()
                }
            }
        })
        </script>
        <style>
        .dynamic-component-demo-tab-button {
          padding: 6px 10px;
          border-top-left-radius: 3px;
          border-top-right-radius: 3px;
          border: 1px solid #ccc;
          cursor: pointer;
          background: #f0f0f0;
          margin-bottom: -1px;
          margin-right: -1px;
        }
        .dynamic-component-demo-tab-button:hover {
          background: #e0e0e0;
        }
        .dynamic-component-demo-tab-button.dynamic-component-demo-active {
          background: #e0e0e0;
        }
        .dynamic-component-demo-tab {
          border: 1px solid #ccc;
          padding: 10px;
        }
        .dynamic-component-demo-posts-tab {
          display: flex;
        }
        .dynamic-component-demo-posts-sidebar {
          max-width: 40vw;
          margin: 0 !important;
          padding: 0 10px 0 0 !important;
          list-style-type: none;
          border-right: 1px solid #ccc;
        }
        .dynamic-component-demo-posts-sidebar li {
          white-space: nowrap;
          text-overflow: ellipsis;
          overflow: hidden;
          cursor: pointer;
        }
        .dynamic-component-demo-posts-sidebar li:hover {
          background: #eee;
        }
        .dynamic-component-demo-posts-sidebar li.dynamic-component-demo-active {
          background: lightblue;
        }
        .dynamic-component-demo-post-container {
          padding-left: 10px;
        }
        .dynamic-component-demo-post > :first-child {
          margin-top: 0 !important;
          padding-top: 0 !important;
        }
        </style>
        </div>     
    </body>
</html>

-----------------------分割线-----------------------------

运行效果图:

在动态组件上使用 keep-alive的更多相关文章

  1. vue深入了解组件——动态组件&异步组件

    一.在动态组件上使用 keep-alive 我们之前曾经在一个多标签的界面中使用 is 特性来切换不同的组件: <component v-bind:is="currentTabComp ...

  2. vue组件---动态组件&异步组件

    (1)在动态组件上使用keep-alive 之前曾经在一个多标签的界面中使用 is 特性来切换不同的组件.接下来简单回顾下 <component>元素是vue 里面的一个内置组件.在里面使 ...

  3. Vue动态组件&异步组件

    在动态组件上使用keep-alive 我们之前曾经在一个多标签的界面中使用is特性来切换不同的组件: Vue.js的动态组件模板 <component v-bind:is="curre ...

  4. 学习笔记:Vue——动态组件&异步组件

    动态组件 01.在动态组件上使用keep-alive,保持组件的状态,以避免反复重渲染导致的性能问题. <!-- 失活的组件将会被缓存!--> <keep-alive> < ...

  5. 深入了解组件- -- 动态组件 & 异步组件

    gitHub地址:https://github.com/huangpna/vue_learn/example里面的lesson11 一 在动态组件上使用keep-alive 在这之前我们已经有学习过用 ...

  6. vue组件上动态添加和删除属性

    1.vue组件上动态添加和删除属性 // 添加 this.$set(this.obj, 'propName', val) // 删除 this.$delete(this.obj, 'propName' ...

  7. C++ 类的动态组件化技术

    序言: N年前,我们曾在软件开发上出现了这样的困惑,用VC开发COM组件过于复杂,用VB开发COM组件发现效率低,而且不能实现面向对象的很多特性,例如,继承,多态等.更况且如何快速封装利用历史遗留的大 ...

  8. vue2入坑随记(二) -- 自定义动态组件

    学习了Vue全家桶和一些UI基本够用了,但是用元素的方式使用组件还是不够灵活,比如我们需要通过js代码直接调用组件,而不是每次在页面上通过属性去控制组件的表现.下面讲一下如何定义动态组件. Vue.e ...

  9. Vue动态组件

    前面的话 让多个组件使用同一个挂载点,并动态切换,这就是动态组件.本文将详细介绍Vue动态组件 概述 通过使用保留的 <component> 元素,动态地绑定到它的 is 特性,可以实现动 ...

随机推荐

  1. mybatis错误 Mapped Statements collection does not contain value for

    java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for 在unit里测试 ...

  2. 【C/C++】金币

    做了一下去年的题目,今年看起来就没这么难了 从上到下的可以从下到上考虑,会简单很多,dp入门 题目 金币 小招在玩一款游戏,在一个N层高的金字塔上,以金字塔顶为第一层,第i层有i个落点,每个落点有若干 ...

  3. 为什么企业全面云化需要IT战略支撑和驱动?

    引子:为什么传统企业全面云化一直磨磨唧唧举步维艰? 笔者将企业上云大体上分为几个阶段: 第一个阶段是基础设施虚拟化.即将应用从物理机搬到(lift and shift migration)虚拟机上.基 ...

  4. 有了代码变更分解提交工具SmartCommit,再也不担心复合提交了

    摘要:文将介绍一个代码提交辅助工具SmartCommit,其主要功能是通过杂糅变更分解算法自动生成分组提交方案,接受开发者的反馈和交互式调整,渐进式地引导和辅助开发者做出符合最佳实践的原子提交. 本文 ...

  5. ciscn_2019_en_3

    例行检查我就不放了,64位的程序放入ida中 可以看到s到buf的距离是0x10,因为puts是遇到\x00截止.而且题目没有限制我们s输入的数量,所以可以通过这个puts泄露出libc的基值 很明显 ...

  6. [BUUCTF]REVERSE——简单注册器

    简单注册器 附件 步骤: apk文件,直接用apkide打开 去找反编译后的文件,反编译后的语言并没有看大懂,网上百度后找到了一个反编的神器jeb,下载地址 用它反编译后按tab,就能看懂代码了,搜索 ...

  7. LuoguP4759 [CERC2014]Sums 题解

    Content 给定 \(t\) 组数据,每组数据给定一个数 \(n\),判断 \(n\) 是否能够分解成连续正整数和,能的话给出最小数最大的方案. 数据范围:\(1\leqslant n\leqsl ...

  8. Java的垃圾回收机制:强制回收System.gc() Runtime.getTime().gc()

    垃圾回收 当引用类型的实体,如对象.数组等不再被任何变量引用的时候.这块占用的内存就成为了垃圾.JVM会根据自己的策略决定是回收内存 注意: 垃圾回收只回收内存中的对象,无法回收物理资源(数据库连接, ...

  9. 为什么需要两次eval才转化为需要的JSON数据,好奇怪

    为什么需要两次eval才转化为需要的JSON数据,好奇怪

  10. 20款GitHub上优秀的Go开源项目

    docker 无人不知的虚拟华平台,开源的应用容器引擎,借助该引擎,开发者可以打包他们的应用,移植到任何平台上. https://github.com/docker/docker 38154 star ...