😄
xlzy520
  • README
  • 执笔
    • 2017
      • 01 网站动态标题的两种方式
      • 02 RN App 外部唤醒踩坑记
    • 2018
      • 01 图片加解密二三事
      • 02 优雅实现 BackTop
      • 03 Vue 一键导出 PDF
      • 04 不一样の烟火
      • 05 Python 之禅
      • 06 Python 文件操作
    • 2019
      • 01 Aurora 食用指南
      • 02 Godaddy 域名找回记事
      • 03 一个接口的诞生
      • 04 SpringMVC 前后端传参协调
      • 05 主题集成友链访问统计功能
      • 06 Github Style 博客主题
      • 07 动态加载 JS 文件
      • 08 WebSocket 心跳重连机制
      • 09 洗牌算法实现数组乱序
      • 10 React Hook 定时器
      • 11 Fetch data with React Hooks
      • 12 字符编码の小常识
      • 13 WSL 安装 Docker 实录
      • 14 Eriri comic reader
  • 书斋
    • ES6 标准入门
      • 01 变量声明与解构赋值
      • 02 语法的扩展
      • 03 数据类型与数据结构
      • 04 Proxy 和 Reflect
      • 05 异步编程 Promise
      • 06 Iterator 和 for of 循环
      • 07 Generator 函数
      • 08 Async 函数
      • 09 Class 类
    • JavaScript 设计模式
      • 01 基础知识
      • 02 设计模式(上)
      • 03 设计模式(下)
      • 04 设计原则和编程技巧
  • 前端
    • JavaScript
      • 01 JavaScript 秘密花园
      • 02 JavaScript 正则技巧
      • 03 从浏览器解析 JS 运行机制
      • 04 Canvas 基础用法
      • 05 Flex 弹性布局
      • 06 Blob Url And Data Url
      • 07 函数节流与函数防抖
      • 08 排序算法初探
    • Node
      • 01 Node Tips
      • 02 七天学会 NodeJS
    • Note
      • 01 Note
      • 02 Snippets
      • 03 Interview
      • 04 Git
      • 05 Docker
      • 06 Line
    • React
      • 01 React Props Children 传值
      • 02 Use a Render Prop!
      • 03 React Hook
      • 04 React 和 Vue 中 key 的作用
    • Vue
      • 01 Vue Tips
      • 02 Vue 构建项目写入配置文件
      • 03 Vue 项目引入 SVG 图标
  • 后端
    • Java
      • 01 浅析 Java 反射
      • 02 Java 服务端分层模型
    • Note
      • 01 Note
      • 02 Linux
      • 03 MySQL
    • Spring
      • 01 Spring Boot
      • 02 Spring Data
      • 03 JPA
      • 04 Swagger
      • 05 AOP
      • 06 SSM
    • Project
      • 01 微信点餐系统
由 GitBook 提供支持
在本页
  • 001 redirect 刷新页面
  • 002 removeRoutes
  • 003 引入 CDN 文件
  • 004 Watch immediate
  • 005 Attrs 和 Listeners
  • 006 Google 桌面通知

这有帮助吗?

  1. 前端
  2. Vue

01 Vue Tips

上一页Vue下一页02 Vue 构建项目写入配置文件

最后更新于5年前

这有帮助吗?

001 redirect 刷新页面

目的是在不刷新页面的情况下更新路由,实现类似 location.reload() 的功能,区别是只更新路由而不是刷新整个页面。关于这个问题的讨论可以查看 Github Issues 。

实现方法是注册一个 redirect 的路由,手动重定向页面到 /redirect 页面,然后再将页面重定向回来,由于页面的 key 发生了变化,从而间接实现了刷新页面组件的效果。

参考 vue-element-admin 的实现方案:

// 1. 手动注册 redirect 路由【redirect.vue】
<script>
export default {
  beforeCreate() {
    const { params, query } = this.$route
    const { path } = params
    this.$router.replace({ path: '/' + path, query })
  },
  render: function(h) {
    return h() // avoid warning message
  }
}
</script>


// 2. 其他页面手动重定向到 '/redirect' 页面
const { fullPath } = this.$route
this.$router.replace({
  path: '/redirect' + fullPath
})

002 removeRoutes

Vue Router 可以通过 addRoutes 来添加动态路由,却没有方法来移除路由。这就是导致一个问题,当用户权限发生变化的时候,或者说用户登出的时候,只能通过刷新页面的方式,才能清空之前注册的路由。作为一个 SPA 应用,刷新页面其实是一种很糟糕的用户体验。

Vue Router 注册的路由信息都是存放在 matcher 之中的,所以如果想清空路由,只需要将 matcher 清空即可。那如何实现呢?首先新建一个空的 Router 实例,将之前路由的 matcher 替换为空实例的 matcher 即可。

参考 vue-element-admin 的实现方案:

const createRouter = () =>
  new Router({
    // mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes
  })

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router

003 引入 CDN 文件

对于项目中用到的一些第三方库,可以通过 CDN 的方式来引入,这样可以增加项目构建的速度,而且如 jsdelivr、unpkg 等 CDN 源提供免费的资源加速与缓存优化。一般的 CDN 资源引入方式是在 index.html 里直接引入外链,但是这种方式不够优雅,通过 webpack 可以用更 Geek 的方式实现。

参考 vue-element-admin 的实现方案:

// vue.config.js
module.exports = {
  productionSourceMap: false,
  chainWebpack(config) {
    const cdn = {
      css: ['//fonts.googleapis.com/css?family=Noto+Serif+SC'],
      js: ['//cdn.jsdelivr.net/npm/animejs@3.0.1/lib/anime.min.js']
    }
    config.plugin('html').tap(args => {
      args[0].cdn = cdn
      return args
    })
  }
}

然后在 public/index.html 中依次注入 css 与 js:

<!DOCTYPE html>
<html lang="en">
  <head>
    <!-- 引入样式 -->
    <% for(var css of htmlWebpackPlugin.options.cdn.css) { %>
    <link rel="stylesheet" href="<%=css%>" />
    <% } %>
  </head>
  <body>
    <div id="app"></div>
    <!-- 引入JS -->
    <% for(var js of htmlWebpackPlugin.options.cdn.js) { %>
    <script src="<%=js%>"></script>
    <% } %>
  </body>
</html>

004 Watch immediate

Vue watch 数据时在实例初始化时并不会执行,只有初始化后更新才会触发。如何让 watch 在初始化时候也执行呢?

其实 watch 的变量可以申明为一个对象,对象提供三个属性 immediate、deep、handler。immediate 表示初始化时是否立即执行。deep 表示是否进行深度监听,如果 watch 一个对象,设置深度监听的话如果对象的某个属性更新的话也会触发。handler 表示更新的回调函数。

watch: {
  query: {
    immediate: true,
    deep: true,
    handler(val) {
      console.log('new val', val)
    }
  }
}

005 Attrs 和 Listeners

如果有 react 使用经验的同学会知道,在 react 中可以通过扩展运算符来一次性传递多个 props,但是在 Vue 中如何实现呢?

// React 中一次性传入多个 props
<Hello {...props} />

在 vue 2.4 版本中新增了两个新特性,v-bind="$attrs" 传递所有属性,v-on="$listeners" 传递所有方法,这两个属性在进行第三方组件封装和写高阶组件时大有妙用。

在高阶组件中,本质是实现一个中间件,将父组件传过来的 props 传递给子组件,如果传递 props 有很多,这两个新属性就派上了用场,这让我们不必要在 $props 中申明方法和属性而可以直接引用。

<Hello v-bind="$attrs" v-on="$listeners" />

006 Google 桌面通知

首先定义插件:

// notification.js
let notification = {}
notification.install = function(Vue, options) {
  Vue.prototype.$notification = (title, options) => {
    // 先检查浏览器是否支持
    if (!('Notification' in window)) {
      alert('很抱歉当前浏览器不支持桌面通知!')
    } else if (Notification.permission === 'granted') {
      // 检查用户是否同意接受通知
      notification = new Notification(title, options)
    } else if (Notification.permission !== 'denied') {
      // 否则我们需要向用户获取权限
      Notification.requestPermission(function(permission) {
        // 如果用户同意,就可以向他们发送通知
        if (permission === 'granted') {
          notification = new Notification(title, options)
        }
      })
    }
  }
}
export default notification

然后注册插件:

// main.js
import Notification from '@/utils/notification'
Vue.use(Notification)

食用方式:

this.$notification('提示', {
  body: '提示内容',
  icon: 'static/icon.png'
})
[Feature] A reload method like location.reload()