vue项目微信端清理缓存问题解决

摘要:
Vue是一个单页前端框架,用于清理缓存。通常的方法是添加html头meta。如果逻辑需要缓存,则不建议使用此方法。代码片段如下:<htmlmanifest=“IGNORE.manifest”>&书信电报;头部>&书信电报;元字符集=“utf-8”>&书信电报;metaname=“viewport”content=“width=设备宽度,初始化

vue为单页面前端框架,清理缓存,常规的方式是添加html头部meta,如果逻辑里面是需要使用缓存的,这种方式不建议使用,代码片段如下:

<html manifest="IGNORE.manifest">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />
    <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
    <meta http-equiv="Pragma" content="no-cache" />
    <meta http-equiv="Expires" content="0" />
......

  第二种方式就是在文件名后添加随机数,可以在路由里面进行设置,但如果路由是需要带参数的,会有影响;

const routes = [
  { path: '/', redirect: '/userInfo'},
  { path: '/home',component:home},
  { path: '/login?123232', component: login, meta: {title: '登录'}},
  { path: '/userInfo', component: userInfo, meta: { requiresAuth: true, title: '账号管理' }},
  { path: '/toggleIdentity',component:toggleIdentity, meta: { requiresAuth: true, title: '身份切换' }},
  { path: '/myTracks', component: myTracks, meta: { requiresAuth: true, title: '我的足迹' }},
  // { path: '/applicationCenter', component: applicationCenter, meta: { requiresAuth: true, title: '应用中心'}},
  { path: '/topicList', component: topicList, meta: { requiresAuth: true, title: '话题探讨' }},
  { path: '/topicList/:id', component: topicDetail, meta: {title: '话题详情'}},
  { path: '/activityList', component: activityList, meta: { requiresAuth: true, title: '精彩活动' }},
  { path: '/interactionList', component: InteractionList, meta: { requiresAuth: true, title: '家校互动' }},
  { path: '/officeMsg', component: OfficeMsg , meta: { requiresAuth: true, title: '办公短信' }},
  { path: '/msgInfo/:messageId/:type/:smsMessageType/:keyTime', component: MsgInfo, meta: { requiresAuth: true, title: '详情' } },
  { path: '/sendMessage', component: SendMessage , meta: { requiresAuth: true, title: '发消息' }},
  { path: '/msgStatusReport/:messageId/:msgType/:keyTime', component: MsgStatusReport , meta: { requiresAuth: true, title: '状态报告' }},
  { path: '/commentList',component:commentList},
  { path: '/DPlayer/:id',component:DPlayer},
  { path: '/application', component: Application , meta: { requiresAuth: true, title: '应用' }}
];

 第三种就是在vue项目里面进行代码添加后缀,以随机数的形式,进入。原理上和第二种方式相似,url地址变化,就不会有上次的缓存了。

import store from './vuex/store'
import cookie from './libs/cookie'
 
// 路由预先判断
router.beforeEach((to, from, next) => {
  // 首先判断是否已有用户登录信息userInfo
  if (store.state.user.info) {
    next()
  } else {
    // 强制给index.html 加上时间戳
    if (document.URL.indexOf('index.html?t=') < 0) {
      let timestamp = (new Date()).valueOf()
      window.location.href = 'http://t.zoukankan.com/index.html?t=' + timestamp + '#' + to.fullPath
    }
    let ua = window.navigator.userAgent.toLowerCase()
    // 判断微信客户端
    if (ua.indexOf('micromessenger') > 1) {
      // 首先获取授权cookie authid
      let authid = cookie.get('authid')
      if (authid) {
        Vue.http.post('/index.php/weixin/Auth/auth', {authid: authid}).then((response) => {
          let res = response.data
          if (res.code === '04') {
            cookie.del('authid')
            window.location.href = 'http://t.zoukankan.com/404.html'
          } else if (res.code === '01') {
            store.dispatch('setUserInfo', res.userInfo)
            next()
          }
        }, (response) => {})
      } else {
        // 强制跳转,授权登录,并设置cookie
        window.location.href = 'http://t.zoukankan.com/index.php/weixin/Auth/redirect?redirect=' + encodeURIComponent(document.URL)
      }
    } else {
      //  非微信客户端
      Vue.http.post('/index.php/weixin/Auth/auth', {openid: cookie.get('openid')}).then((response) => {
        let res = response.data
        if (res.code === '04') {
          cookie.del('authid')
          next()
          // window.location.href = 'http://t.zoukankan.com/index.php/weixin/Auth/redirect?redirect=' + encodeURIComponent(document.URL)
        } else if (res.code === '01') {
          store.dispatch('setUserInfo', res.userInfo)
          next()
        }
      }, (response) => {})
    }
  }
})

免责声明:文章转载自《vue项目微信端清理缓存问题解决》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇魅族MX3 Flyme3.0找回手机功能支持远程拍照密码错两次自动拍照QLabel显示图像下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Vue 开发规范目录及说明

Vue 开发规范目录及说明 Vue 开发规范目录及说明 命名规范 普通变量命名规范 常量 组件命名规范 views 下的文件命名 结构化规范 目录文件夹及子文件规范 vue 文件基本结构 多个特性的元素规范 元素特性的顺序 组件选项顺序 为组件样式设置作用域 注释规范 务必添加注释列表 单行注释 多行注释 模块 指令规范 Props 规...

如何运行vue项目

转载:如何运行vue项目 目录(?)[-] 安装cnpm 安装vue-cli脚手架构建工具 用vue-cli构建项目 安装项目所需的依赖 运行项目 在师兄的推荐下入坑vue.js ,发现不知如何运行GitHub上的开源项目,很尴尬。通过查阅网上教程,成功搭建好项目环境,同时对前段工程化有了朦朦胧胧的认知,因此将环境搭建过程分享给大家。 首先,列...

Vue3.0 全面探索 基于 Visual Studio Code 的代码片段开发插件

Vue3 Snippets for Visual Studio Code Vue3 Snippets源码 Vue3 Snippets下载 This extension adds Vue3 Code Snippets into Visual Studio Code. 这个插件基于最新的 Vue3 的 API 添加了 Code Snippets。 Snip...

前端跨域

跨域是指一个域下的文档或脚本试图去请求另一个域下的资源。也就是说如果协议,域名,或者端口有一个不同就是跨域。 那么为什么要用跨域? 其实是因为浏览器出于安全考虑,我们都知道浏览器有同源策略。如果没有同源策略的情况下,A网站可以被任意来源的Ajax访问到内容,如果当前A网站还处于登录态,那么对方就可以通过Ajax获得A网站的任何消息。当然跨域可以用来房子CS...

Vue2、websocket 与node.js接口 本地测试

Vue2、websocket 与node.js接口 本地测试 1.  安装vue-native-websocket模块 2.  yarn add vue-native-websocket  或者用  npm install vue-native-websocket --save   3. 在main.js中引入websocket import websoc...

Vue 基础篇一

Vue框架介绍 之前大家学过HTML,CSS,JS,JQuery,Bootstrap,,现在我们要接触一个新的框架Vue Vue是一个构建数据驱动的web界面的渐进式框架 目标是通过尽可能简单的API实现响应式的数据绑定和组合的视图组件. 能够构建复杂的单页面应用.现在我们开始认识一下Vue // HTML 页面 <div id="app">...