📜  Node.js urlSearchParams.get() 方法

📅  最后修改于: 2022-05-13 01:56:30.702000             🧑  作者: Mango

Node.js urlSearchParams.get() 方法

urlSearchParams.get()方法是url模块中URLSearchParams类的内置应用程序编程接口,用于获取 URL 搜索参数对象中存在的特定名称条目的值。

句法:

const urlSearchParams.get( name )

参数:此方法将名称作为参数。

返回值:此方法返回 url 搜索参数对象中存在的特定名称条目的值。

下面的程序说明了 Node.js 中urlSearchParams.get()方法的使用:

示例 1:文件名:app.js

// Node.js program to demonstrate the 
// URLSearchParams.get() method
   
// Importing the module 'url'
const http = require('url');
  
// Creating and initializing 
// URLSearchParams object
const params = new URLSearchParams();
  
// Appending value in the object
params.append('A', 'Book');
params.append('B', 'Pen');
params.append('C', 'Pencile');
  
// Getting the value for entry 'A'
// by using get() api
const value = params.get('A');
  
// Display the result
console.log("value for A is " + value);

使用以下命令运行app.js文件:

node app.js

输出:

value for A is Book

示例 2:文件名:app.js

// Node.js program to demonstrate the 
// URLSearchParams.get() method 
   
// Importing the module 'url'
const http = require('url');
  
// Creating and initializing
// URLSearchParams object
const params = new URLSearchParams();
  
// Appending value in the object
params.append('A', 'Book');
params.append('B', 'Pen');
params.append('C', 'Pencile');
  
// Getting the value for entry 'A'
// by using get() api
const value = params.get('a');
  
// Display the result
console.log("value for a is " + value);

使用以下命令运行app.js文件:

node app.js

输出:

value for a is null

参考: https://nodejs.org/dist/latest-v14.x/docs/api/url.html#url_urlsearchparams_get_name