当前位置:网站首页>vue--vuerouter缓存路由组件
vue--vuerouter缓存路由组件
2022-06-26 16:50:00 【春风霓裳】
作用:能保存组件原来的数据!!!(注意:时同一个父路由路径下切换才行)
在其父组件中使用 标签,include的值为组件名!!!
<ul>
<li>
<router-link active-class="active" to="/home/message">message</router-link>
</li>
<li>
<router-link active-class="active" to='/home/news'>news</router-link>
</li>
</ul>
<!-- include的值为组件名!!! -->
<keep-alive include="News">
<router-view></router-view>
</keep-alive>
缓存多个组件路由
<keep-alive :include="['News','Message']">
<router-view></router-view>
</keep-alive>
代码演示:
<template> <div class="Home"> <h1>Home</h1> <ul> <li> <router-link active-class="active" to="/home/message">message</router-link> </li> <li> <router-link active-class="active" to='/home/news'>news</router-link> </li> </ul> <!-- include的值为组件名!!! --> <keep-alive include="News"> <router-view></router-view> </keep-alive> </div> </template> <script> export default {
name:'Home', data(){
return{
}
}, methods:{
}
}
</script> <style scoped> .Home{
background-color: aqua;
}
</style>
路由index.js
// 该文件用于专门创建路由器 import VueRouter from 'vue-router' import About from '../pages/About' import Home from '../pages/Home' import News from '../pages/News' import Message from '../pages/Message' import Detail from '../pages/Detail' // 创建并暴露一个路由器 export default new VueRouter({
routes:[
{
name:'guanyu', path:'/about', component:About, children:[
{
// 二级路由路径不能加/ path:'message', component:Message, children:[
{
name:'xiangqing', // path:'detail/:id/:title', path:'detail', component:Detail, // props的第一种写法,值为对象, // 该对象中的所有key-value都会以props的形式传给Detail组件 // props:{
a:1,b:'hello'}
// props的第二种写法,值为布尔值,若该布尔值为真 // 就会把该路由组件收到的所有params参数,以props的形式传给Detail组件 // props:true // props的第三种写法,值为函数 props($route){
return {
id:$route.query.id,title:$route.query.title}
}
}
]
}
]
},
{
path:'/home', component:Home, children:[
{
// 二级路由路径不能加/
path:'news',
component:News
},
{
// 二级路由路径不能加/ path:'message', component:Message, children:[
{
name:'xiangqing', // path:'detail/:id/:title', path:'detail', component:Detail, // props的第一种写法,值为对象, // 该对象中的所有key-value都会以props的形式传给Detail组件 // props:{
a:1,b:'hello'}
// props的第二种写法,值为布尔值,若该布尔值为真 // 就会把该路由组件收到的所有params参数,以props的形式传给Detail组件 // props:true // props的第三种写法,值为函数 props($route){
return {
id:$route.query.id,title:$route.query.title}
}
}
]
}
]
}
]
})
结果:
边栏推荐
- The student record consists of student number and academic performance. The data of n students have been stored in the a structure array to find out the student record with the lowest performance
- When I was in the library, I thought of the yuan sharing mode
- Cache breakdown! Don't even know how to write code???
- SIGIR 2022 | 港大等提出超图对比学习在推荐系统中的应用
- Greenplum数据库故障分析——semop(id=2000421076,num=11) failed: invalid argument
- Science | giant bacteria found in mangroves challenge the traditional concept of nuclear free membrane
- Notes on flowus
- Teach you to learn dapr - 9 Observability
- 链游系统开发技术方案设计丨NFT链游系统开发流程及源码
- Today, I met a "migrant worker" who took out 38K from Tencent, which let me see the ceiling of the foundation
猜你喜欢
Turtle cartography
Ndroid development from introduction to mastery Chapter 2: view and ViewGroup
Play with Linux and easily install and configure MySQL
Discussion: the next generation of stable coins
Leetcode 1170. Frequency of occurrence of the minimum letter of the comparison string (yes, solved)
分布式缓存/缓存集群简介
Microservice architecture practice: business management background and SSO design: SSO design
Microservice architecture practice: business management background and SSO design, SSO client design
The latest masterpiece of Alibaba, which took 182 days to produce 1015 pages of distributed full stack manual, is so delicious
进军AR领域,这一次罗永浩能成吗?
随机推荐
Redis' 43 serial cannons, try how many you can carry
proxy
Today, I met a "migrant worker" who took out 38K from Tencent, which let me see the ceiling of the foundation
[qt learning notes]qt inter thread data communication and data sharing
The texstudio official website cannot be opened
What is the difference between digital collections and NFT
[matlab project practice] prediction of remaining service life of lithium ion battery based on convolutional neural network and bidirectional long short time (cnn-lstm) fusion
Secrets of gear contract
Platform management background and merchant menu resource management: Design of platform management background data service
Turtle cartography
Live broadcast preview | how can programmers improve R & D efficiency? On the evening of June 21, the video number and station B will broadcast live at the same time. See you or leave!
Convert the decimal positive integer m into the number in the forward K (2 < =k < =9) system and output it in bits
Don't believe it, 98% of programmers are like this
去中心化NFT交易协议将击败OpenSea
Teach you to learn dapr - 4 Service invocation
Classical synchronization problem
Programmer interview guide - self introduction
What is the preferential account opening policy of securities companies now? Is it safe to open an account online now?
Introduction to distributed cache / cache cluster
Toupper function