0
点赞
收藏
分享

微信扫一扫

Axure是什么软件?全方位解读助力设计入门

三维控件研究 2024-11-10 阅读 2

效果:

充实自我:

keep-alive缓存路由页面案例:

const routes = [
{
path: '/home',
component: HomePage,
meta: { keepAlive: true }
},
{
path: '/about',
component: AboutPage,
meta: { keepAlive: false }
}
]

tab栏缓存的和没有缓存的代码:

<template>
<div>
<!-- Tab 切换 -->
<div class="tabs">
<button @click="currentTab = 'tab1'" :class="{ active: currentTab === 'tab1' }">Tab 1</button>
<button @click="currentTab = 'tab2'" :class="{ active: currentTab === 'tab2' }">Tab 2</button>
<button @click="currentTab = 'tab3'" :class="{ active: currentTab === 'tab3' }">Tab 3</button>
</div>

<!-- 使用 keep-alive 缓存 Tab 内容 -->
<!-- :key 绑定 currentTab 使得每次切换时强制 Vue 重新渲染组件 -->
<!-- :include="['Tab2', 'Tab3']" / :exclude=['Tab1'] -->
<keep-alive :include="['Tab2', 'Tab3']" :max="2">
<component :is="tabComponents[currentTab]" :key="currentTab"></component>
</keep-alive>
</div>
</template>

<script setup>
import { ref } from 'vue'

// Tab 组件
import Tab1 from './Tab1.vue'
import Tab2 from './Tab2.vue'
import Tab3 from './Tab3.vue'

// Tab 组件映射
const tabComponents = {
tab1: Tab1,
tab2: Tab2,
tab3: Tab3
};

// 当前选中的 Tab
const currentTab = ref('tab1');
</script>

<style scoped>
.tabs button {
margin: 10px;
padding: 10px;
cursor: pointer;
}

.tabs button.active {
background-color: #007bff;
color: white;
}
</style>
//Tab1页面
<template>
  <div style="display: flex;flex-direction: column; justify-content: space-between; width: 50%; margin: 0 auto;">
    <h1>没有被缓存</h1>
    <h2>当前计数:{{ count }}</h2>
    <button style="width: 100px ;height:30px;" @click="handel">+5</button>
    <button style="width: 100px ;height:30px;" @click="delhandel">-5</button>
  </div>
</template>

<script setup>
defineProps({
  name: {
    type: String,
    default: 'Tab1' // 为组件定义 name
  }
});
// 这里可以放置 Tab 2 的逻辑,数据或者其他操作
const count = ref(0)
const handel = () => {
  count.value += 5;
}
const delhandel = () => {
  count.value -= 5;
}
</script>

<style scoped>
/* Tab1 的样式 */
div {
  padding: 20px;
  background-color: red;
}
</style>
//Tab2页面
<template>
  <div style="display: flex; flex-direction: column; justify-content: space-between;width: 50%; margin: 0 auto;">
    <h1>被缓存</h1>
    <h2>当前计数:{{ count }}</h2>
    <button style="width: 100px ;height:30px;" @click="handel">+5</button>
    <button style="width: 100px ;height:30px;" @click="delhandel">-5</button>
  </div>
</template>

<script setup>
defineProps({
  name: {
    type: String,
    default: 'Tab2' // 为组件定义 name
  }
});
// 这里可以放置 Tab 2 的逻辑,数据或者其他操作
const count = ref(0)
const handel = () => {
  count.value += 5;
}
const delhandel = () => {
  count.value -= 5;
}
import { onActivated, onDeactivated } from 'vue';

onActivated(() => {
  console.log('Tab2/Tab3 激活,缓存加载');
});

onDeactivated(() => {
  console.log('Tab2/Tab3 停用,缓存卸载');
});
</script>


<style scoped>
/* Tab2 的样式 */
div {
  padding: 20px;
  background-color: pink;
}
</style>
//Tab3页面
<template>
  <div style="display: flex;flex-direction: column; justify-content: space-between;width: 50%; margin: 0 auto;">
    <h1>被缓存</h1>
    <h2>当前计数:{{ count }}</h2>
    <button style="width: 100px ;height:30px;" @click="handel">+1</button>
    <button style="width: 100px ;height:30px;" @click="delhandel">-1</button>
  </div>
</template>

<script setup>
defineProps({
  name: {
    type: String,
    default: 'Tab3' // 为组件定义 name
  }
});
// 将 count 初始化为数字类型
const count = ref(0); // 初始值应该是数字类型

const handel = () => {
  count.value++;  // 加 1
};

const delhandel = () => {
  count.value--;  // 减 1
};
import { onActivated, onDeactivated } from 'vue';

onActivated(() => {
  console.log('Tab2/Tab3 激活,缓存加载');
});

onDeactivated(() => {
  console.log('Tab2/Tab3 停用,缓存卸载');
});
</script>

<style scoped>
/* Tab2 的样式 */
div {
  padding: 20px;
  background-color: green;
}
</style>
举报

相关推荐

0 条评论