新型实用的常见js数据处理方法

125 阅读1分钟

1.地图请求方法

   import fetch from "node-fetch";

  arcGISAttrQuery(where) {
      // let url = 'http://192.168.10.182/MapServer/3/query';
      let url = this.queryServerUrl + '/query';
      let queryUrl = `${url}?where=${where}&f=pjson`
      return new Promise((resolve, reject) => {
        fetch(queryUrl).then(res => {
          return res.json()
        }).then(res => {
          resolve(res)
        }).catch(err => {
          reject(err)
        })
      })
    },

2.获取url上的参数

export const getCode=function (url) {
  url=url?url:window.location.href
  if(url.indexOf('?')==-1){
    url = '?' + window.location.hash.split('?')[1]
  }
  let obj = {};
  let reg = /[?&][^?&]+=[^?&]+/g;
  let arr = url.match(reg);
  if (arr) {
    arr.forEach((item) => {
      let tempArr = item.substring(1).split('=');
      let key = decodeURIComponent(tempArr[0]);
      let val = decodeURIComponent(tempArr[1]);
      obj[key] = val;
    });
  }
  return obj;
}

3.一个数组切割成多个数组方法

export const sliceArray=function (array, size) {
  let result = [];
  for (let x = 0; x < Math.ceil(array.length / size); x++) {
    let start = x * size;
    let end = start + size;
    result.push(array.slice(start, end));
  }
  return result;
}

4.Vue.directive()定义一个全局指令

import Vue from 'vue';
//使用Vue.directive()定义一个全局指令
//1.参数一:指令的名称,定义时指令前面不需要写v-
//2.参数二:是一个对象,该对象中有相关的操作函数
//3.在调用的时候必须写v-
Vue.directive('drag', {
  //1.指令绑定到元素上回立刻执行bind函数,只执行一次
  //2.每个函数中第一个参数永远是el,表示绑定指令的元素,el参数是原生js对象
  //3.通过el.focus()是无法获取焦点的,因为只有插入DOM后才生效
  bind: function () {},
  //inserted表示一个元素,插入到DOM中会执行inserted函数,只触发一次
  inserted: function (el,binding) {
    let dom = el
    if(binding.value){
      dom = document.getElementById(binding.value)
      dom.style.cursor = 'move'
    }
    dom.onmousedown = function (e) {
      var disx = e.pageX - el.offsetLeft;
      var disy = e.pageY - el.offsetTop;
      document.onmousemove = function (e) {
        el.style.left = e.pageX - disx + 'px';
        el.style.top = e.pageY - disy + 'px';
      }
      document.onmouseup = function () {
        document.onmousemove = document.onmouseup = null;
      }
    }
  },
  //当VNode更新的时候会执行updated,可以触发多次
  updated: function () { }
})