收集、管理和分享有用的代码片段,提高开发效率
使用Fetch API发送POST请求
// 使用Fetch API发送POST请求
fetch('/api/data', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
key: 'value'
})
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
在JavaScript中获取当前页面URL
// 获取当前页面URL
const currentUrl = window.location.href;
TypeScript接口定义
interface User {
id: number;
name: string;
email: string;
age? : number; // Optional
}
function printUser(user: User): void {
console.log(`Name: ${user.name} Email: ${user.email}`);
}
TypeScript类示例
class Person {
private name: string;
age: number;
constructor(name: string, age: number) {
this.name = name;
this.age = age;
}
describe(): string {
return `${this.name} is ${this.age} years old`;
}
}
const alice = new Person('Alice', 30);
console.log(alice.describe());
Promise异步处理
const fetchData = (url) => {
return new Promise((resolve, reject) => {
fetch(url)
.then(response => response.json())
.then(data => resolve(data))
.catch(error => reject(error));
});
};
fetchData('https://api.example.com/data')
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
TypeScript接口定义
interface User {
id: number;
name: string;
email: string;
age? : number; // Optional
}
function printUser(user: User): void {
console.log(`Name: ${user.name} Email: ${user.email}`);
}
高效管理您的代码片段,提高开发效率
通过关键字、语言或分类快速查找代码片段,支持模糊搜索和过滤功能
支持多种编程语言的语法高亮,使代码更加清晰易读
随时随地访问您的代码片段库,支持桌面和移动设备
浏览最受欢迎的代码分类