zoukankan      html  css  js  c++  java
  • 使用vue来做局部刷新

    我们都知道,vue的组件化是他最强大的核心所在,路由也是特别可爱的一部分,但是路由适合一些大型的组件,看url路径的时候会出现变化,但是有时候我们想要一些小的局部小刷新,这个时候就需要用到它的动态组件了:

    Vue 自身保留的 <component> 元素,可以将组件动态绑定到 is 特性上,从而很方便的实现动态组件切换

    代码如下:slotDome.vue:

    <template>
      <div>
        <slot></slot>
        <slot name="wise" class="demo"></slot>
    
        <el-radio-group v-modal="tabView">
          <el-radio-button label="simple1" @click="toSim(1)">
            <button>页面一</button></el-radio-button>
          <el-radio-button label="simple2" @click="toSim(2)"><button>页面二</button></el-radio-button>
        </el-radio-group>
        <keep-alive>
          <component :is="tabView"></component>
        </keep-alive>
      </div>
    </template>
    <style lang="stylus" rel="stylesheet/stylus">
      el-radio-button
        &:hover
          cursor pointer
    </style>
    <script>
      import simple1 from "./simple/simple1.vue";
      import simple2 from "./simple/simple2.vue";
      export default{
        data(){
          return {
            tabView: "simple1"
          }
        },
        methods: {
          toSim(index){
            this.tabView = `simple${index}`;
          }
        },
        components: {
          simple1,
          simple2
        }
      }
    </script>
    

      

      simple1.vue:

    <template>
      <div>
        这是页面一
        <input type="text">
      </div>
    </template>
    

      

      simple2.vue:

    <template>
      <div>
        这是页面二
        <input type="text">
      </div>
    </template>
    

      

      

    上例中,当 tabView 的值改变,<component> 就会渲染对应的组件,和路由的效果十分类似,但是地址栏并没有发生改变

    但这样一来,每次切换组件都会重新渲染,无法保留组件上的数据。这时可以使用 keep-alive 将组件保留在内存中,避免重新渲染

    页面效果如下:

  • 相关阅读:
    org.hibernate.annotationexception no identifier specified for entity
    PL/SQL Developer 中文乱码解决
    cron表达式
    mysql远程连接的设置
    linux查看端口对应的程序及pid
    安卓开发分享功能,分享到facebook网页上不显示图片的问题
    win7下解压安装mysql的方法
    总结一下论文写作过程中的一些东西
    java中可以让程序暂停几秒执行的代码
    Neo4j图数据库使用
  • 原文地址:https://www.cnblogs.com/mmykdbc/p/8037620.html
Copyright © 2011-2022 走看看