Home > Web Front-end > JS Tutorial > Introduction to the function that can convert objects in js into url parameters (code example)

Introduction to the function that can convert objects in js into url parameters (code example)

不言
Release: 2018-09-17 15:31:45
Original
4160 people have browsed it

This article brings you an introduction to the function that can convert objects in js into url parameters (code examples)). It has certain reference value. Friends in need can refer to it. I hope it will be useful to you. Helps.

This function is used when I write a management background based on Vue ElementUI. There are two ways to use it:

The most common one is to encapsulate a jsFunction

 /**
   * 对象转url参数
   * @param {*} data
   * @param {*} isPrefix
   */
 urlencode (data, isPrefix) {
    isPrefix = isPrefix ? isPrefix : false
    let prefix = isPrefix ? '?' : ''
    let _result = []
    for (let key in data) {
      let value = data[key]
      // 去掉为空的参数
      if (['', undefined, null].includes(value)) {
        continue
      }
      if (value.constructor === Array) {
        value.forEach(_value => {
          _result.push(encodeURIComponent(key) + '[]=' + encodeURIComponent(_value))
        })
      } else {
        _result.push(encodeURIComponent(key) + '=' + encodeURIComponent(value))
      }
    }

    return _result.length ? prefix + _result.join('&') : ''
  }
Copy after login

During Vue component development, I wrote like this

Write a tool file utils.js, introduce it into Vue’s main.js as a toolkit, and attach it to Vue prototype, so that you can use this.$utils in each component to use some tool functions inside

utils.jsFile

const utils = {
  /**
   * 对象转url参数
   * @param {*} data
   * @param {*} isPrefix
   */
  urlencode (data, isPrefix = false) {
    let prefix = isPrefix ? '?' : ''
    let _result = []
    for (let key in data) {
      let value = data[key]
      // 去掉为空的参数
      if (['', undefined, null].includes(value)) {
        continue
      }
      if (value.constructor === Array) {
        value.forEach(_value => {
          _result.push(encodeURIComponent(key) + '[]=' + encodeURIComponent(_value))
        })
      } else {
        _result.push(encodeURIComponent(key) + '=' + encodeURIComponent(value))
      }
    }

    return _result.length ? prefix + _result.join('&') : ''
  },

  // ....其他函数....

}

export default utils
Copy after login

main.js File

import Vue from 'vue'
import App from './App.vue'
import utils from '@/utils/utils'

// ...其他代码...

Vue.prototype.$utils = utils

// ...其他代码...
Copy after login

You can write like this when using it

// ....其他代码

this.$utils.urlencode(this.params)

// ...其他代码...
Copy after login

The above is the detailed content of Introduction to the function that can convert objects in js into url parameters (code example). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template