Ahooks Vue

0.15.1 · active · verified Sun Apr 19

ahooks-vue is a comprehensive collection of Vue Composition API hooks, designed to simplify common logic and stateful operations in Vue 2 (specifically 2.7+) and Vue 3 applications. Many of its hooks are direct ports and adaptations from the popular React hooks library, ahooks, offering a familiar API for developers transitioning or working across both ecosystems. Currently at version 0.15.1, the library sees a consistent, though not strictly scheduled, release cadence, with minor versions frequently bringing new features and bug fixes. Its key differentiators include broad compatibility achieved through `vue-demi`, enabling a single codebase for both major Vue versions, and full TypeScript support, providing predictable static types for enhanced developer experience and code maintainability. It aims to provide a robust and production-ready set of utilities for common patterns like state management, async requests, DOM operations, and more, similar to `vue-use` but with a distinct API influence from `ahooks`.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to fetch data asynchronously using the `useRequest` hook with Axios in a Vue 3 component. It shows manual execution, loading state, error handling, and data display.

import { defineComponent, ref } from 'vue';
import { useRequest } from 'ahooks-vue';
import axios from 'axios';

interface User {
  id: number;
  name: string;
  email: string;
}

const fetchUser = async (userId: number): Promise<User> => {
  const response = await axios.get(`https://jsonplaceholder.typicode.com/users/${userId}`);
  return response.data;
};

export default defineComponent({
  setup() {
    const userId = ref(1);
    const { data, loading, error, run } = useRequest(() => fetchUser(userId.value), {
      manual: true, // Prevents immediate execution on component mount
      onSuccess: (result) => {
        console.log('User fetched successfully:', result);
      },
      onError: (err) => {
        console.error('Error fetching user:', err);
      },
    });

    const fetchUserData = () => run();

    return {
      data,
      loading,
      error,
      fetchUserData
    };
  },
  template: `
    <div style="padding: 20px; font-family: sans-serif;">
      <h1>User Data with useRequest</h1>
      <p>This demonstrates fetching user data using ahooks-vue's useRequest hook.</p>
      <button @click="fetchUserData" :disabled="loading" style="padding: 10px 15px; background-color: #007bff; color: white; border: none; border-radius: 4px; cursor: pointer;">
        {{ loading ? 'Loading...' : 'Fetch User (ID: 1)' }}
      </button>
      <div v-if="error" style="color: red; margin-top: 10px;">Error: {{ error.message }}</div>
      <div v-if="data" style="background-color: #f8f9fa; padding: 15px; border-radius: 4px; margin-top: 10px;">
        <p><strong>ID:</strong> {{ data.id }}</p>
        <p><strong>Name:</strong> {{ data.name }}</p>
        <p><strong>Email:</strong> {{ data.email }}</p>
      </div>
      <div v-else-if="!loading" style="margin-top: 10px; color: #6c757d;">Click the button to fetch user data.</div>
    </div>
  `
});

view raw JSON →