本文共 1426 字,大约阅读时间需要 4 分钟。
当组件在keep-alive中被切换时,缓存一次后mounted就不执行了,而activated和deactivated这两个钩子函数会被执行。
activated:被 keep-alive 缓存的组件激活时调用。 deactivated:被 keep-alive 缓存的组件停用时调用。keep-alive还可以接受两个参数,include和exclude,允许组件有条件地缓存。
二者都可以用字符串或正则表达式。 include:只有匹配的组件会被缓存。 exclude:任何被匹配的组件将不会被缓存结果:
代码:
keep-alive用法
{ {item.name}}
路由:
主要要设置 meta:{ keepAlive:true }export default new Router({ mode: 'history', base: process.env.BASE_URL, routes: [ { path: '/', name: 'home', component: Home, children:[ { path: '/', name:'HelloWorld', component: () => import( './components/HelloWorld.vue'), meta:{ keepAlive:true } }, { path: '/HelloWorld', name:'HelloWorld', component: () => import( './components/HelloWorld.vue'), meta:{ keepAlive:true } }, { path: '/tabContent', name:'tabContent', component: () => import( './components/tabContent.vue'), meta:{ keepAlive:true } }, ] }, ]})
注意:匹配首先检查组件自身的 name 选项,如果 name 选项不可用,则匹配它的局部注册名称 (父组件 components 选项的键值)。匿名组件不能被匹配。
keep-alive中include用法
{ {item.name}}
转载地址:http://hwrgn.baihongyu.com/