代码片段仓库

收集、管理和分享有用的代码片段,提高开发效率

代码片段

AJAX请求 JavaScript

使用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));
获取当前URL JavaScript

在JavaScript中获取当前页面URL

// 获取当前页面URL
const currentUrl = window.location.href;
TS接口 TypeScript

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}`);
}
TS类 TypeScript

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());
JS Promise JavaScript

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));
TS接口 TypeScript

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}`);
}

为什么选择CodeSnippets?

高效管理您的代码片段,提高开发效率

智能搜索

通过关键字、语言或分类快速查找代码片段,支持模糊搜索和过滤功能

语法高亮

支持多种编程语言的语法高亮,使代码更加清晰易读

多设备同步

随时随地访问您的代码片段库,支持桌面和移动设备

热门分类

浏览最受欢迎的代码分类