肖强的博客 肖强的博客
Home
Guide
Web Document
  • Plugins
  • Case
  • Animation
Components
  • Categories
  • Tags
  • Archives
Website Tools

xiaoQiang

因为爱所以爱
Home
Guide
Web Document
  • Plugins
  • Case
  • Animation
Components
  • Categories
  • Tags
  • Archives
Website Tools
  • 常用插件
    • dayjs
    • vue-clipboard2
    • qs
    • number-precision
    • url-parse
      • 安装
      • Usage
        • Url.set(key, value)
        • Url.toString()
    • clipboard
    • js-cookie
    • accounting
  • views
xiaoQiang
2021-09-27

url-parse

url-parse

url-parse是一个轻量的处理url的库

# 安装

npm install url-parse -S

# Usage

'use strict';

var Url = require('url-parse');

要解析URL,只需使用需要的URL调用' URL '方法 转换成物体

var url = new Url('https://github.com/foo/bar');

new关键字是可选的,但它将为您节省额外的函数调用。
构造函数接受以下参数 :

  • url (String): 表示绝对或相对URL的字符串.
  • baseURL (Object | String): An object or string representing the base URL to use in case url is a relative URL. This argument is optional and defaults to location (opens new window) in the browser.
  • parser (Boolean | Function): This argument is optional and specifies how to parse the query string. By default it is false so the query string is not parsed. If you pass true the query string is parsed using the embedded querystringify module. If you pass a function the query string will be parsed using this function.

如上所述,我们也支持Node.js接口,所以你也可以这样使用 :

'use strict';

var parse = require('url-parse')
  , url = parse('https://github.com/foo/bar', true);

返回的' url '实例包含以下属性 :

  • protocol: The protocol scheme of the URL (e.g. http:).
  • slashes: A boolean which indicates whether the protocol is followed by two forward slashes (//).
  • auth: Authentication information portion (e.g. username:password).
  • username: Username of basic authentication.
  • password: Password of basic authentication.
  • host: Host name with port number.
  • hostname: Host name without port number.
  • port: Optional port number.
  • pathname: URL path.
  • query: 包含查询字符串的已解析对象,除非解析设置为false .
  • hash: The "fragment" portion of the URL including the pound-sign (#).
  • href: The full URL.
  • origin: The origin of the URL.

Note that when url-parse is used in a browser environment, it will default to using the browser's current window location as the base URL when parsing all inputs. To parse an input independently of the browser's current URL (e.g. for functionality parity with the library in a Node environment), pass an empty location object as the second parameter:

var parse = require('url-parse');
parse('hostname', {});

# Url.set(key, value)

A simple helper function to change parts of the URL and propagating it through all properties. When you set a new host you want the same value to be applied to port if has a different port number, hostname so it has a correct name again and href so you have a complete URL.

var parsed = parse('http://google.com/parse-things');

parsed.set('hostname', 'yahoo.com');
console.log(parsed.href); // http://yahoo.com/parse-things

It's aware of default ports so you cannot set a port 80 on an URL which has http as protocol.

# Url.toString()

返回的url对象带有一个自定义的toString方法,在调用时再次生成完整的URL。该方法接受一个额外的函数,它将为您字符串化查询字符串。如果你不提供功能,我们将使用我们的默认方法

var location = url.toString(); // http://example.com/whatever/?qs=32
#plugins
上次更新: 2021-10-05 17:37:55
number-precision
clipboard

← number-precision clipboard→

最近更新
01
drag
12-22
02
入场/出场05
10-16
03
入场/出场04
10-16
更多文章>
icon组件不能用
时间左侧 © 2021-2021 时间右侧
冀公网安备 13108202000744号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
欢迎来到我的家
Back