The difference between OptionsAPI and CompositionAPI in code usage and logic
1. Differences in code usage
(I) Component definition structure
-
OptionsAPI
- Define components in the form of object literals. The object contains multiple properties, each attribute corresponding to a different function. For example:
export default { data() { return { message: 'Hello, OptionsAPI' }; }, methods: { sayHello() { (); } }, mounted() { (); } };
- here
data
、methods
、mounted
The attributes of the component define the data, methods, life cycle hooks, etc., which exist independently of each other under a large object structure. -
CompositionAPI
- use
setup
Functions act as the entry point of component logic, define various logics inside the function and return values for use by the template. For example:
- use
import { ref } from 'vue'; export default { setup() { const message = ref('Hello, CompositionAPI'); const sayHello = () => { (); }; sayHello(); return { message, sayHello }; } };
- exist
setup
In the function, responsive data (such asref
The created data), methods, etc. are logically compactly combined, and then the data and methods that need to be used in the template are exposed by returning values.
(II) Definition and use of responsive data
OptionsAPI
- exist
data
Define responsive data in the function. For example:
export default { data() { return { count: 0 }; }, methods: { increment() { ++; } } };
- Responsive data passes
this
The keywords aremethods
、watch
etc. access and modify it in other properties. Herethis
Point to component instances, this method may occur when used in multi-layer nested functions or callback functionsthis
Point to confusion.- CompositionAPI
- use
ref
orreactive
Functions to define responsive data. - For example
ref
:
import { ref } from 'vue'; export default { setup() { const count = ref(0); const increment = () => { ++; }; return { count, increment }; } };
- Or use
reactive
(Used to create responsive data of object type):
import { reactive } from 'vue'; export default { setup() { const state = reactive({ count: 0 }); const increment = () => { ++; }; return { state, increment }; }; };
- Responsive data passes
value
property(ref
Type) or directly manipulate object properties (reactive
Type) to access and modify, avoidingthis
Point to the problem.
(III) Implementation of logical multiplexing
-
OptionsAPI
- Mainly by
mixins
to implement logical multiplexing. For example, create amixin
Object:
- Mainly by
const myMixin = { data() { return { sharedData: 'This is shared data' }; }, methods: { sharedMethod() { ('This is a shared method'); } } }; export default { mixins: [myMixin], // Other logic of the component itself data() { return { componentData: 'This is component - specific data' }; }, mounted() { (); } };
butmixins
There are some problems such as possible naming conflicts (if multiplemixins
There are attributes or methods with the same name) and when used in components, it is difficult to trace the origin of a certain attribute or method.
-
CompositionAPI
- Logical multiplexing is achieved through custom combination functions.
For example:
import { ref } from 'vue'; const useCounter = () => { const count = ref(0); const increment = () => { ++; }; return { count, increment }; }; export default { setup() { const { count, increment } = useCounter(); return { count, increment }; } };
- Custom combinatorial functions can be reused in multiple components, with clear code structure and easy to understand and maintain.
2. Logical differences
(I) Logical cohesion
-
OptionsAPI
- Related logic is scattered in different option properties. For example, the initialization logic associated with a data might be in
data
In the operation logic of the data ismethods
In the data change listening logic iswatch
middle. This dispersed structure makes it necessary to switch between multiple attributes to understand the complete logical flow when dealing with complex functions.
- Related logic is scattered in different option properties. For example, the initialization logic associated with a data might be in
-
CompositionAPI
- exist
setup
Relevant logic can be combined in functions. For example, for a counter function, it can besetup
The function defines responsive data, methods of operating data, and related life cycle hooks (if needed), making the logic of the entire function more cohesive and easy to understand and maintain.
- exist
(II) Support logic for Type-Script
-
OptionsAPI
- When using TypeScript in OptionsAPI, you need to define the type separately for each option attribute. For example,
data
The data defined in the function needs to be defined separately.methods
The methods in this article also need to define the parameters and return values, etc. This makes the type definition scattered throughout the component definition and the code structure is relatively complex. - Here is an example of OptionsAPI using TypeScript:
- When using TypeScript in OptionsAPI, you need to define the type separately for each option attribute. For example,
export default { data(): { message: string; } { return { message: 'Hello' }; }, methods: { sayHello(): void { (); } } };
-
CompositionAPI
- Since logic is organized in a functional form,
setup
The overall type definition can be more convenient in the function. OKsetup
The input parameters and return values of the function are type-defined, and the types can be well defined in custom combination functions. This method is more closely integrated with TypeScript's function type system, and the type definition of the code is more concise and clear. - For example:
- Since logic is organized in a functional form,
import { ref } from 'vue'; const useCounter = (): { count: { value: number }; increment: () => void; } => { const count = ref(0); const increment = () => { ++; }; return { count, increment }; }; export default { setup(): { count: { value: number }; increment: () => void; } { const { count, increment } = useCounter(); return { count, increment }; } };
This is the end of this article about the difference between Vue OptionsAPI and CompositionAPI. For more related contents of Vue OptionsAPI and CompositionAPI, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!