index.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import {
  2. setStorage,
  3. getStorage,
  4. prePage
  5. } from "@/util/index.js"
  6. import {
  7. IMG_URL
  8. } from '@/env'
  9. import {mapActions, mapState} from 'vuex'
  10. import tools from '@/common/tools.js'
  11. import api from '@/common/request/index'
  12. // 默认回调
  13. const callbackFun = () => {}
  14. // 自定义插件初始化
  15. const install = (Vue, vm) => {
  16. // 设置本地静态化
  17. Vue.prototype.$setStorage = setStorage
  18. // 获取本地静态化
  19. Vue.prototype.$getStorage = getStorage
  20. // 获取上一页
  21. Vue.prototype.$prePage = prePage
  22. // 全局绑定请求方法
  23. Vue.prototype.$api = api
  24. // 方法延迟保护函数
  25. Vue.prototype.$debounce = function debounce(fn, delay = 1000) {
  26. // 定时器,用来 setTimeout
  27. var timer
  28. // 返回一个函数,这个函数会在一个时间区间结束后的 delay 毫秒时执行 fn 函数
  29. return function() {
  30. // 保存函数调用时的上下文和参数,传递给 fn
  31. var context = this
  32. var args = arguments
  33. // 每次这个返回的函数被调用,就清除定时器,以保证不执行 fn
  34. clearTimeout(timer)
  35. // 当返回的函数被最后一次调用后(也就是用户停止了某个连续的操作),
  36. // 再过 delay 毫秒就执行 fn
  37. timer = setTimeout(function() {
  38. fn.apply(context, args)
  39. }, delay)
  40. }
  41. }
  42. // 全局混入方法
  43. Vue.mixin({
  44. data() {
  45. },
  46. computed:{
  47. ...mapState({
  48. }),
  49. $imgUrl(){
  50. return IMG_URL
  51. }
  52. },
  53. methods: {
  54. // 统计
  55. $st(callback = callbackFun, event = "0") {
  56. // 调用回调
  57. callback.call(this)
  58. }
  59. }
  60. })
  61. Vue.prototype.$tools = tools
  62. }
  63. export default {
  64. install
  65. }