Composition API implements logic reuse steps:
For example, define a method to get the current mouse position The first one is to use the useMousePosition defined by ref directly: This way, both exports and imports can be destructured at will // useMousePosition.js import { ref, onMounted, onUnmounted } from 'vue' // 1. Define a function, extract logic, and name it useXXX function useMousePosition() { // Use ref to define const x = ref(0) const y = ref(0) function update(e) { console.log(x.value, y.value); x.value = e.pageX y.value = e.pageY } onMounted(() => { console.log('Start listening for mouse movement events'); window.addEventListener('mousemove', update) }) onUnmounted(() => { console.log('Remove mouse scroll event monitoring'); window.removeEventListener('mousemove', update) }) return { x, y } } // Export this function export default useMousePosition <!-- This method can be called in any component--> <template> <p>mouse position: {{x}}, {{y}}</p> </template> <script> import useMousePosition from './useMousePosition' export default { name: 'MousePosition', setup() { // useMousePosition uses ref to define variables, which can be deconstructed const { x, y } = useMousePosition() console.log(x, y) return { x, y } } } </script> The second method is to use reactive to define the mouse coordinate object. This export method cannot be destructured when imported into a component. import { onMounted, onUnmounted, reactive } from 'vue' export function useMousePosition2() { // Use reactive to define const mouse = reactive({ x: 0, y: 0 }) function update(e) { mouse.x = e.pageX mouse.y = e.pageY } onMounted(() => { console.log('Start listening for mouse movement events'); window.addEventListener('mousemove', update) }) onUnmounted(() => { console.log('Remove mouse scroll event monitoring'); window.removeEventListener('mousemove', update) }) return { mouse } } <template> <!-- Use object method to display information --> <p>mouse position2: {{mouse.x}}, {{mouse.y}}</p> </template> <script> import { useMousePosition2 } from './useMousePosition' export default { name: 'MousePosition', setup() { // useMousePosition2 is defined using reactive, so deconstruction is not possible const { mouse } = useMousePosition2() return { mouse } } } </script> The third method is to use toRefs Using this method, you can deconstruct reactive objects into ref objects. export function useMousePosition3() { // Use reactive to define const mouse = reactive({ x: 0, y: 0 }) function update(e) { mouse.x = e.pageX mouse.y = e.pageY } onMounted(() => { console.log('Start listening for mouse movement events'); window.addEventListener('mousemove', update) }) onUnmounted(() => { console.log('Remove mouse scroll event monitoring'); window.removeEventListener('mousemove', update) }) // Here, use toRefs to deconstruct into ref object return toRefs(mouse) } <template> <p>mouse position: {{x}}, {{y}}</p> </template> <script> import { useMousePosition3 } from './useMousePosition' export default { name: 'MousePosition', setup() { // Use reactive to define the mouse coordinate object, and then deconstruct it into a ref object through toRefs const { x, y } = useMousePosition() console.log(x, y) return { x, y } } } </script> All three methods can be implemented, but when we generally use them, we will return the ref object, so it is recommended to use the first and third methods and try not to use the second method. This is the end of this article about how to implement logic reuse with Vue3 composition API. For more information about Vue3 composition API logic reuse, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Problems encountered when installing mysql8.0.15 winx64 on Win10 and connecting to the server
>>: Tutorial analysis of quick installation of mysql5.7 based on centos7
Table of contents 1. Custom import in packaging t...
Judging from the results, there is no fixed patte...
Operating system win10 MySQL is the 64-bit zip de...
Table of contents 1. Import files 2. HTML page 3....
Tutorial Series MySQL series: Basic concepts of M...
background When performing a SQL query, I tried t...
There are many MySQL variables, some of which are...
This article example shares the specific code of ...
Putting input and img on the same line, the img ta...
There are two common ways to download files in da...
Because of network isolation, MySQL cannot be ins...
Table of contents 1. Overview 1. Principle 2. Imp...
Although Microsoft provides T4 templates, I find ...
Let's first understand a wave of concepts, wh...
Syntax composition: 1 Annotation information 2 Co...