1、router.js(路由)
//主要部分
meta:{ keepAlive: true}
import myDemand from '../views/keep/myDemand.vue'
import AboutView from '../views/keep/AboutView.vue'
const routes = [
{
path: '/myDemand',
name: 'myDemand',
meta:{
keepAlive: true,
},
component: myDemand
},
{
path: '/AboutView',
name: 'AboutView',
component: AboutView
},
]
2、HomeView.vue (主页)
//主要部分
<keep-alive>
<router-view v-if="$route.meta.keepAlive"></router-view>
</keep-alive>
<router-view v-if="!$route.meta.keepAlive"></router-view>
//完整代码
<template>
<el-container>
<el-aside width="200px">
<el-menu class="el-menu-vertical-demo" @select="handleSelect" background-color="#ccc" text-color="#000"
active-text-color="#000">
<el-menu-item index="myDemand">
<span slot="title">导航一</span>
</el-menu-item>
<el-menu-item index="AboutView">
<span slot="title">导航二</span>
</el-menu-item>
</el-menu>
</el-aside>
<el-main>
<keep-alive>
<router-view v-if="$route.meta.keepAlive"></router-view>
</keep-alive>
<router-view v-if="!$route.meta.keepAlive"></router-view>
</el-main>
</el-container>
</template>
<script>
export default {
name: 'HomeView',
components: {},
data () {
return {}
},
methods: {
handleSelect (key) {
this.$router.push({ path: `/${key}` })
}
}
}
</script>
<style lang='less' scoped>
.el-container {
height: 100%;
.el-aside {
background-color: #D3DCE6;
color: #333;
text-align: center;
line-height: 200px;
height: 100%;
.el-menu {
height: 100%;
}
}
}
</style>
3、myDemand.vue (有缓存页面)
<template>
<div>
<el-row>
<el-col :span="4">
<el-input v-model="input" placeholder="请输入内容"></el-input>
</el-col>
<el-col :span="12">
<el-select v-model="value" placeholder="请选择">
<el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
</el-option>
</el-select>
</el-col>
<el-button type="primary">搜索</el-button>
</el-row>
</div>
</template>
<script>
export default {
name: 'myDemand',
components: {},
data () {
return {
input: '',
options: [{
value: '选项1',
label: '黄金糕'
}, {
value: '选项2',
label: '双皮奶'
}],
value: '',
}
},
activated () {
// 页面激活后的钩子函数,一进入页面就触发,如果想进入页面重新请求写在该钩子中
console.log("测试")
// equipmentType(this, {})
// .then((res) => {
// if (res.status === 1) {
let history = this.input;
this.input = '';
this.$nextTick(() => {
this.input = history
})
// }
// }).finally(() => {
// this.searchLoading = false;
// });
},
}
</script>
<style>
.el-table {
margin-top: 50px;
}
</style>
4、AboutView.vue(无缓存页面)
<template>
<div class="about">
<h1>This is an about page</h1>
<el-input v-model="input" placeholder="请输入内容"></el-input>
</div>
</template>
<script>
export default {
data () {
return {
input: ''
}
}
}
</script>