Reference

Reference Composables

Composables

OtherEvergreenPublic

The Options Object pattern

export function useMouse(options) {
  const {
    asArray = false,
    throttle = false,
  } = options

  // ...
}

How to parse the options argument?

Here’s how you would implement the options object pattern in a composable:

export function useTitle(newTitle, options) {
    const {
      observe = false,
      titleTemplate = '%s',
    } = options;

    // ...
  }

Here, we can accept newTitle (our required argument), and then the last argument is the options object. The next step is to destructure the options object. By destructuring, we can access all the values, and clearly provide defaults for each possible option.

(17) Composable Vue - Anthony Fu - VueDay 2021 - YouTube