----------------------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. Playing with Destructors in C++

    Predict the output of the below code snippet. 1 #include <iostream> 2 using namespace std; 3 4 ...

  2. shell脚本实现网站日志分析统计

    如何用shell脚本分析与统计每天的访问日志,并发送到电子邮箱,以方便每天了解网站情况.今天脚本小编为大家介绍一款不错的shell脚本,可以实现如上功能. 本脚本统计了:1.总访问量2.总带宽3.独立 ...

  3. mysql数据库备份脚本一例

    例子,mysql数据库备份脚本.vim mysql.sh #!/bin/bash DAY=`date +%Y-%m-%d` //日期以年月日显示并赋予DAY变量 SIZE=`du -sh /var/l ...

  4. Linux:expr、let、for、while、until、shift、if、case、break、continue、函数、select

    1.expr计算整数变量值 格式 :expr arg 例子:计算(2+3)×4的值 1.分步计算,即先计算2+3,再对其和乘4 s=`expr 2 + 3` expr $s \* 4 2.一步完成计算 ...

  5. Spring Batch(0)——控制Step执行流程

    Conditional Flow in Spring Batch I just announced the new Learn Spring course, focused on the fundam ...

  6. Charles ios设备抓包

    在Mac下做开发,用Fiddler抓包由于离不开Windows比较痛苦,还好有Charles,到官网http://www.charlesproxy.com/可下载到最新版本(若不支持rMBP可拖到Re ...

  7. Node.js 中文乱码解决

    Node.js 中文乱码解决 Node.js 支持中文不太好(实际上是Javascript支持),见<Node.js开发指南>. 要想Node.js正常显示中文,需要两点: 1.js文件保 ...

  8. 删除空行(嵌套)(Power Query 之 M 语言)

    数据源: "姓名""基数""个人比例""个人缴纳""公司比例""公司缴纳"&qu ...

  9. 深入浅出Mysql索引优化专题分享|面试怪圈

    文章纲要 该文章结合18张手绘图例,21个SQL经典案例.近10000字,将Mysql索引优化经验予以总结,你可以根据纲要来决定是否继续阅读,完成这篇文章大概需要25-30分钟,相信你的坚持是不负时光 ...

  10. CF363A Soroban 题解

    Content 给出一个数 \(n\),请你用算盘来表示 \(n\). 这里的算盘和普通的算盘一样,只不过竖着摆放罢了.左边只有一个珠子,每个珠子表示 \(5\):右边有四个珠子,每个珠子表示 \(1 ...