VUE组件——动态组件、组件缓存

news/2024/7/10 2:05:14 标签: vue, js, vue.js

动态组件:多个组件同时使用同一个挂载点,并动态的进行切换,就是动态组件

<template>
  <div>
      <button @click="comName = 'page1'">page1</button>
      <button @click="comName = 'page2'">page2</button>

      <p>下面显示注册组件-动态切换:</p>
      <div>
          <component :is="comName"></component>
          
          //vue内置component组件, 配合is属性, 设置要显示的组件名字
          
      </div>
  </div>
</template>

<script>
import page1 from './components/page1'
import page2 from './components/page2'
export default {
    data(){
        return {
            comName: "page1"
        }
    },
    components: {
        page1,
        page2
    }
}

组件缓存:当组件动态切换较为频繁时,组件会经常创建和销毁,影响性能。
Vue内置了 keep-alive 组件,可以让被keep-alive包裹的组件存储在内存中而不被销毁。

<template>
  <div>
     <keep-alive>
          <component :is="comName"></component>
     </keep-alive>  
  </div>
</template>

补充:被 keep-alive 包裹的组件会有两个生命周期

  • activated - 激活状态
  • deactivated - 失去激活状态

同时有两个对应的钩子方法:
activated - 激活时触发
deactivated - 失去激活时触发


http://www.niftyadmin.cn/n/498449.html

相关文章

Vue组件——组件插槽

组件插槽&#xff1a;vue提供的组件插槽&#xff0c;允许开发者在封装组件时&#xff0c;将不确定的需要额外自定义的部分定义为插槽。 1、匿名插槽&#xff1a; 组件内插槽占位&#xff1a; <template><div><h4>组件插槽</h4>//<p>这是组件插…

代码托管的网站

https://bitbucket.org/

c#将数据库二进制图片保存到硬盘上

1.建立一个aspx页面&#xff0c;copy html代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns"http://www.w3.org/1999/xhtml" >&…

【NetApp】启用smb2.0

NetApp控制器的message log中&#xff0c;出现如下报错&#xff1a;[NetAppA:cifs.server.errorMsg:error]: CIFS: Error for server \\WIN-OM6C2DSK37Q: Error while negotiating protocol with server STATUS_IO_TIMEOUT. 解决方法&#xff0c;参见链接&#xff1a;https://k…

C#向数据库写二进制以及读出

1.C#把文件当作二进制流写进数据库 SqlConnection myconnection new SqlConnection(strsql); myconnection.Open(); SqlCommand mycommand new SqlCommand(); FileInfo myfile new FileInfo("D://**.*"); FileStream mystream …

反射获取对象的属性值

1 > bean 对象 package com.whbs.bean; public class UserBean { private Integer id ; private int age ; private String name ; private String address ; public UserBean(){ System. out .println( " 实例化 " ); } public Integer getId() { return id ; } …

【转】TCP为什么是个可靠的协议

作者&#xff1a;平生不缺金原文地址&#xff1a;http://www.cnblogs.com/wxyy/p/3337368.html一直以来&#xff0c;我们都被告知TCP是可靠的。但为什么是可靠的&#xff0c;很多人都会说“三次握手、四次挥手”。然后我们就进入一个误区&#xff1a;TCP可靠是因为它在建立链路…

mssql数据库系统崩溃后的一般处理步骤与方法

mssql数据库系统崩溃后的一般处理步骤 情况描述&#xff1a; SQL Server 2000崩溃&#xff0c;重新安装数据库。 有以下准备&#xff1a; 1, 三个系统库&#xff08;master,msdb,model&#xff09;的完全备份: 2 两个用户数据库(user01,user02)的备份&#xff08;周日的完全备份…