Vue 中keep-alive组件将会被缓存

动态包裹哈
<keep-alive>
  <component :is="comName"></component>
</keep-alive>

子组件

<template>
	  <div>
	    <h2>我是登录组件</h2>
	  </div>
</template>
	
<template>
	  <div>
	    <h2>我是注册组件</h2>
	  </div>
</template>
---------------
<template>
  <div class="mett-page">
    <h2>遇见问题</h2>
    <!-- 推荐这种写法-->
    <ul class="tab-tilte">
      <li
        :key="index"
        v-for="(title,index) in tabTitle"
        @click="getclcik(index)"
        :class="{active:cur==index}"
      >{{title}}</li>
    </ul>

    <div class="tab-content">
      <div v-for="(m,index) in tabMain" :key="index" v-show="cur==index">{{m}}</div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tabTitle: ["标题一", "标题二", "标题三", "标题四"],
      tabMain: ["内容一", "内容二", "内容三", "内容四"],
      cur: 0 //默认选中第一个tab
    };
  },
  methods: {
    getclcik(value) {
      this.cur = value;
    }
  }
};
</script>

<style scoped>
.mett-page .tab-tilte {
  display: flex;
  list-style: none;
}
.mett-page .tab-tilte > li {
  width: 50px;
  height: 40px;
}
</style>

引入组件##

<template>
  <!-- is属相的使用 -->
  <div class="box">
    <div class="link-a" @click="comName='login'">登录</div>
    <div class="link-a" @click="comName='resgister'">注册</div>

    <div class="link-a" @click="comName='mett'">遇见问题</div>
    <keep-alive>
      <component :is="comName"></component>
    </keep-alive>
  </div>
</template>

<script>
import login from "../../components/logincom/login";
import resgister from "../../components/logincom/register";
import mett from "../../components/logincom/mett";

export default {
  data() {
    return {
      comName: "login"
    };
  },
  components: {
    resgister,
    login,
    mett
  }
};


</script>

<style  scoped>
.box {
  display: flex;
}
.link-a {
  width: 80px;
  height: 40px;
  text-align: center;
  line-height: 40px;
  background: pink;
  margin-left: 20px;
}
</style>

posted @   南风晚来晚相识  阅读(440)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
点击右上角即可分享
微信分享提示