【Vue】Re18 Router 第五部分(KeepAlive)
一、KeepAlive概述
默认状态下,用户点击新的路由时,是访问新的组件
那么当前组件是会被销毁的,然后创建新的组件对象出来
如果某些组件频繁的使用,将造成内存空间浪费,也吃内存性能
所以需求是希望组件能被缓存起来,不是立即销毁
生命周期的创建函数 create(); 和销毁函数 destory(); 都将反复调用
二、使用
只需要把router-view做为keep-alive的子元素就行了
<template>
<div id="app">
<router-link to="/home" tag="button" >去首页</router-link>
<router-link to="/about" tag="button" >去关于</router-link>
<router-link :to="/user/+username" tag="button" >用户管理</router-link>
<router-link :to="ppp" tag="button" >profile</router-link>
<!-- <button @click="toProfile" >profile</button>-->
<!-- <button @click="toHome">首页</button>-->
<!-- <button @click="toAbout">关于</button>-->
<keep-alive>
<router-view></router-view>
</keep-alive>
<p>
<button @click="getRouterInstance">获取Router对象</button>
<button @click="getCurrentRouteInstance">获取Route对象</button>
</p>
</div>
</template>
如果组件的周期不再销毁,那么生命状态则发生了改变
在访问时是被激活的状态
如果离开了组件时,则是非激活状态
对应了两个生命周期函数:
activated () {
// todo ...
}
deactivated () {
// todo ...
}
注意!!!上述的函数仅对keep-alive处理的组件有效
三、关于重定向路由BUG问题
/router/index.js
父级路由
/* 重定向首页路由配置 */
{
path : '', /* 缺省值默认指向 '/' */
redirect : '/home',
},
子级路由
children : [ /* 设置子路由 */
{
path : '', /* 这个缺省默认/home */
redirect : 'news',
},
重定向home的时候触发子路由的重定向
向下继续重定向到/home/news
解决方案:
移除子路由的重定向,在组件初始化时重定向一次,后续不再重定向
还有要记录用户之前访问的组件的路由,回到之前的父组件时访问的子组件
<template>
<div>
<h3>这是首页Home组件</h3>
<p>
首页Home组件的内容 <br>
<router-link to="/home/news">新闻列表</router-link>
<router-link to="/home/messages">消息列表</router-link>
</p>
<router-view></router-view>
</div>
</template> <script>
export default {
name: "Home",
created() { },
data () {
return {
path : '/home/news'
}
},
activated() {
this.$router.push(this.path);
},
// deactivated() {
// this.path = this.$route.path;
// }
beforeRouteLeave (to, from, next) {
this.path = this.$route.path;
next();
}
}
</script> <style scoped> </style>
四、Keep-Alive的两个属性
<keep-alive
include="Home,Message"
exclude="News,Profile"
>
<router-view></router-view>
</keep-alive>
include表示需要缓存在里面的组件
exclude表示排除,不要缓存的组件
默认是使用正则表达式,符合正则规则的组件名称,就会把该组件选中
也可以是直接写组件的名称表示,注意不要有空格
组件的名称就是这个:
用途主要是为了排除特定不需要缓存的组件,一般需要缓存的不需要填写属性表示了
【Vue】Re18 Router 第五部分(KeepAlive)的更多相关文章
- Vue.js(23)之 keepAlive和activated
阅读: vue中前进刷新.后退缓存用户浏览数据和浏览位置的实践 keep-alive 组件级缓存 keep-alive <keep-alive> 包裹动态组件时,会缓存不活动的组件实例,而 ...
- 三、vue之router
三.vue之router 此时vue的脚手架.创建项目已经完成. ... vue的运行流程 index.html-->main.js-->App.vue-->router/index ...
- Vue中router两种传参方式
Vue中router两种传参方式 1.Vue中router使用query传参 相关Html: <!DOCTYPE html> <html lang="en"> ...
- 四 Vue学习 router学习
index.js: 按需加载组件: const login = r => require.ensure([], () => r(require('@/page/login')), 'log ...
- Vue基础系列(五)——Vue中的指令(中)
写在前面的话: 文章是个人学习过程中的总结,为方便以后回头在学习. 文章中会参考官方文档和其他的一些文章,示例均为亲自编写和实践,若有写的不对的地方欢迎大家和我一起交流. VUE基础系列目录 < ...
- vue 中router.go;router.push和router.replace的区别
vue 中router.go:router.push和router.replace的区别:https://blog.csdn.net/div_ma/article/details/79467165 t ...
- 【vue】 router.beforeEach
import store from '@/store' const Vue = require('vue') const Router = require('vue-router') Vue.use( ...
- vue & this.$router.resolve
vue & this.$router.resolve gotoAutoUpdate (query = {}) { const { href } = this.$router.resolve({ ...
- Vue中router路由的使用、router-link的使用(在项目中的实际运用方式)
文章目录 1.先看router中的index.js文件 2.router-link的使用 3.实现的效果 前提:router已经安装 1.先看router中的index.js文件 import Vue ...
- vue中router使用keep-alive缓存页面的注意事项
<keep-alive exclude="QRCode"> <router-view></router-view> </keep-aliv ...
随机推荐
- 剑指Offer-48.不用加减乘除做加法(C++/Java)
题目: 写一个函数,求两个整数之和,要求在函数体内不得使用+.-.*./四则运算符号. 分析: 将加法还原成最原始的计算方法,也就是分别计算进位和非进位的和,最后再将这两部分合起来,便是和.我们可以利 ...
- ASP.NET MVC 查询加分页
使用了LinqKit.PagedList.Mvc.EntityFramework 等DLL 直接使用nuget安装即可. 1.表模型: using System.ComponentModel.Data ...
- EF 从设计器改为 DB First时遇到 Keyword not supported: 'data source'.
EF 从设计器改为 DB First时遇到 Keyword not supported: 'data source'. 解决方法: 把providerName="System.Data.En ...
- 使用 OpenTelemetry 构建可观测性 02 - 埋点
这是讲解 OpenTelemetry 系列博客的第二篇.在上一篇博客中,我们介绍了 OpenTelemetry 是什么以及由什么组成.现在我们将讨论如何使用 OTel 准确收集遥测数据和链路追踪数据. ...
- Mysql常见安装失败的解决方法
问题一:安装时出现Initializing database失败 解决方法: 1.关闭安装页面并卸载MySQL Installer与MySQL Server (如果卸载不掉需要重启电脑) 2.查看并勾 ...
- java8 多条件的filter过滤
java8 多条件的filter过滤 package com.example.core.mydemo.java; import java.io.Serializable; import java.ti ...
- Navicat Premium v16.0.6 绿色破解版
这里版本:Navicat Premium v16.0.6.0 ,这个是绿色版,不需要安装,启动Navicat.exe即可用 破解工具:NavicatKeygenPatch(其它版本也能破解) 1.下载 ...
- 使用nc进行tcp测速
# server nc -l IP PORT > /dev/null eg: nc -l 192.168.144.1 8080 > /dev/null # client bs单位块大小 c ...
- Windows下USB声卡音量调整
买了一个绿联的USB声卡, 但是默认的音量太大了,最低音量都响的不行. 查了一下, 发现了一个叫EqualizerAPO的软件可以调整输出设备的音量. https://equalizerapo.com ...
- RabbitMQ 3.7.9版本中,Create Channel超时的常见原因及排查方法
在RabbitMQ 3.7.9版本中,Create Channel超时的常见原因及排查方法如下: 常见原因 网络问题: 网络延迟或不稳定可能导致通信超时. 网络分区(network partition ...