As the business continues to evolve, front-end projects tend to become more and more complex, and the way we used options to define components in the past may become less and less readable as we iterate through the functionality. When we add additional functionality to a component, we usually need to modify (initData, attached, computed, etc.) multiple blocks of code; and in some cases, it clearly makes more sense to organize the code by functionality and facilitate fine-grained code reuse.
The Composition API provides a set of methods corresponding to the key that defines the component options to define the properties and methods of the component, allowing developers to organize the code by logical correlation, thus improving the readability and maintainability of the code.
Usage
NPM
1
npm i --save san-composition
Basic usage
When defining a component, we usually need to define templates, initialize data, define methods, add lifecycle hooks, and so on. When defining a component using the Composition API, instead of declaring properties and methods using an options object, we use the methods corresponding to each option to solve the definition of various properties and methods of the component.
Note: The Composition API methods can only be executed during the first function argument of the defineComponent method.
Defining Template
Use the template method to define the template for the component.
1 2 3 4 5 6 7 8 9 10 11 12
import san from'san'; import { defineComponent, template } from'san-composition';
The return value of data is an object that contains get, set, merge, splice and other methods. We can use the methods on the object to get and modify the data.
Defining Computed Data
Use the computed method to define a computed data item.
The API related to lifecycle hooks is named by prefixing the corresponding hook with on, so it corresponds to the component’s lifecycle hooks one by one.
Option API
Hooks in Composition API
construct
onConstruct
compiled
onCompiled
inited
onInited
created
onCreated
attached
onAttached
detached
onDetached
disposed
onDisposed
updated
onUpdated
error
onError
A complete example
The following example shows how to define components using the Composition API.
The point of using the Composition API is to define the data, methods, lifecycle runtime logic, etc. by function. There is no point in using the Composition API for its own sake if you declare an entire component in a complete definition function.
This article gives some guidance on the use of the composition API in two subsections with a simple example.
Composition explains how to transform a component declared using class into an implementation using the compositional API
Reusability builds on the previous section by describing how to break down functions by function and implement reuse
Composition
Suppose we want to develop a contact list. In terms of business logic, the component has the following functions.
A list of contacts, and view and favorite operations.
A list of favorites, as well as view and unfavorite operations.
Filtering contacts by form.
We’ll build two versions of the same component: one with the Class API, and the other with the Composition API.
As components become more feature-rich, the logic of the Class API becomes more and more diffuse, and we often need to jump through multiple modules to read the implementation of a feature, making the code less readable. Next, we use the Composition API to organize the code by function.
We don’t recommend using this in the Composition API, it can cause some confusion, but sometimes you may have to use it, so be careful not to use the arrow function in the corresponding method.