Home > Article > Web Front-end > How to use hooks in Vue3 projects
The hooks function in vue3 is equivalent to the mixin in vue2. The difference is that hooks are functions.
The hooks function in vue3 can improve the reusability of code and can be used in different components.
hooks function can be used with mixin, but it is not recommended.
First of all, we don’t care about hooks. Ha, let’s write a small function first, which is to get the width and height value of the page. This is a common case to explain hooks. They are all veteran actors. , let’s do one too.
I won’t be verbose, let’s just write the code.
<template> <h4>hooks</h4> <p>页面宽度: {{screen.width}}</p> <p>页面高度: {{screen.height}}</p> <el-button @click="getWH">获取页面的宽高</el-button> </template> <script setup> import { reactive } from 'vue' const screen = reactive({ width: 0, height: 0 }) const getWH = () => { screen.width = document.documentElement.clientWidth screen.height = document.documentElement.clientHeight } </script> <style scoped> </style>
The above code is actually very simple. There are two labels to display the length and width of the visible page, and then there is a button to get the latest length and width for display.
This function can be implemented smoothly. If we need to implement this function on another page, it is also very simple. Just copy the above code directly to another page that needs to be implemented.
But
Have you found a problem? If you need a page, copy it once. If you need a page, copy it once. If there are a hundred pages, copy a hundred. Again, one or two lines of code is okay. If it is a super huge tool class, then if it is implemented like this, it will be too complicated, and it is not easy to implement. So how to solve this problem? Ah hahahaha, that’s right guys, it’s hooks.
For the above case, we use hooks to simply implement it.
First, we create a hooks folder under the src folder.
Create a file in the hooks folder, named useScreenWh.js
File
The next step is very simple. We put the code to obtain the visual interface into this js file, then export it and use it on other pages.
import { reactive } from 'vue' export default function () { // 导出一个默认方法 // 创建一个对象,保存宽度和高度值 const screen = reactive({ width: 0, height: 0 }) // 创建一个方法,获取可视化界面的宽度和高度值 const getWH = () => { screen.width = document.documentElement.clientWidth screen.height = document.documentElement.clientHeight } return { screen, getWH } // 方法返回宽高值 }
Then you can use it by importing it into the file where you need to use hooks.
<template> <h4>hooks</h4> <p>页面宽度: {{screen.width}}</p> <p>页面高度: {{screen.height}}</p> <el-button @click="getWH">获取页面的宽高</el-button> </template> <script setup lang="ts"> // 导入 hooks import screenWH from '../hooks/useScreenWh.js' // 因为 screenWH 是一个导出的方法,所以需要调用一下子,然后顺便解构一下就可以在模板使用了。 let { screen, getWH } = screenWH() </script> <style scoped> </style>
Okay, let’s save it and see the effect.
The above is the detailed content of How to use hooks in Vue3 projects. For more information, please follow other related articles on the PHP Chinese website!