Skip to content

Extend Form API

FormCreate's API is highly extensible, allowing you to create custom operations and functions to meet complex business requirements. Use the formCreate.extendApi method to add new methods and properties to the API object.

Get API

Before using extended APIs, you need to obtain the API object. The API object can be obtained through events, validation functions, hook functions, etc.

For detailed instructions, please refer to: How to Get API

Basic Usage

js
import formCreate from '@form-create/element-ui';

formCreate.extendApi((api) => {
  return {
    customMethod() {
      // Execute custom operation
      console.log('This is a custom API method');
    },
    // Custom HTTP request method
    async customRequest(url, options = {}) {
      // Implement custom request logic
      const response = await fetch(url, options);
      return response.json();
    },

    // Custom utility method
    formatDate(date, format = 'YYYY-MM-DD') {
      // Format date
      return new Date(date).toLocaleDateString();
    },

    // Custom data processing method
    processData(data) {
      // Process data
      return data.map(item => ({ ...item, processed: true }));
    }
  };
});

Using Extended API

Extended APIs can be used in form events, validation functions, and anywhere the api object can be accessed:

js
// Use in events
function handleClick($inject) {
  const api = $inject.api;

  // Call extended method
  api.customMethod();

  // Use extended request method
  api.customRequest('/api/data').then(data => {
    console.log('Data:', data);
  });
}

This lets you uniformly use extended APIs throughout the application, improving code maintainability and reusability.