您好,登錄后才能下訂單哦!
這篇文章主要介紹“Vue怎么自定義hook函數(shù)”的相關(guān)知識(shí),小編通過(guò)實(shí)際案例向大家展示操作過(guò)程,操作方法簡(jiǎn)單快捷,實(shí)用性強(qiáng),希望這篇“Vue怎么自定義hook函數(shù)”文章能幫助大家解決問(wèn)題。
什么是hook?
本質(zhì)是一個(gè)函數(shù),把 setup 函數(shù)中使用的 Composition API (組合API)進(jìn)行了封裝
類(lèi)似于 vue2.x 中的 mixin
自定義 hook 的優(yōu)勢(shì):
復(fù)用代碼,讓 setup 中的邏輯更清楚易懂
首先我們做一個(gè)功能,鼠標(biāo)點(diǎn)擊屏幕,獲取坐標(biāo):
<template> <h3>當(dāng)前鼠標(biāo)的坐標(biāo)是:x:{{ point.x }},y:{{ point.y }}</h3> </template> <script> import {onMounted, onBeforeUnmount,reactive} from 'vue' export default { name: 'Demo', setup() { let point = reactive({ x: 0, y: 0 }) function savePoint(event) { point.x = event.pageX; point.y = event.pageY; } onMounted(() => { window.addEventListener("click",savePoint) }) onBeforeUnmount(()=>{ window.removeEventListener("click",savePoint) }) return { point, } }, } </script>
然后改用使用 hooks,在 src 下新建 hooks 文件夾,增加 usePoint.js
import {onBeforeUnmount, onMounted, reactive} from "vue/dist/vue"; function savePoint() { let point = reactive({ x: 0, y: 0 }) function savePoint(event) { point.x = event.pageX; point.y = event.pageY; } onMounted(() => { window.addEventListener("click",savePoint) }) onBeforeUnmount(()=>{ window.removeEventListener("click",savePoint) }) return point } export default savePoint;
或者簡(jiǎn)寫(xiě):
...... export default function() { ...... }
在 Demo.vue 中使用:
<template> <h3>當(dāng)前鼠標(biāo)的坐標(biāo)是:x:{{ point.x }},y:{{ point.y }}</h3> </template> <script> import usePoint from "@/hooks/usePoint"; export default { name: 'Demo', setup() { let point = usePoint() return { point } }, } </script>
hooks 下新建 useRequest.ts
由于用到了 axios,所以安裝axios:npm install axios
import {ref} from "vue"; import axios from "axios"; export default function <T>(url: string) { const loading = ref(true); const data = ref<T | null>(null); const errorMsg = ref(''); axios.get(url).then(response => { loading.value = false data.value = response.data }).catch(error => { loading.value = false errorMsg.value = error.message || '未知錯(cuò)誤' }) return { loading, data, errorMsg } }
App.vue:
<template> <h4 v-if="loading">加載中...</h4> <h4 v-else-if="errorMsg">錯(cuò)誤信息:{{errorMsg}}</h4> <!-- 對(duì)象 --> <ul v-else> <li>{{data.name}}</li> <li>{{data.address}}</li> <li>{{data.distance}}</li> </ul> <!-- 數(shù)組 --> <ul v-for="item in data" :key="item.name"> <li>{{item.name}}</li> <li>{{item.address}}</li> <li>{{item.distance}}</li> </ul> </template> <script lang="ts"> import {defineComponent, watch} from 'vue'; import useRequest from "@/hooks/useRequest"; export default defineComponent({ setup() { // 定義接口 interface IAddress{ name:string, address:string, distance:string } //const {loading,data,errorMsg} = useRequest<IAddress>("./address.json")//獲取對(duì)象數(shù)據(jù) const {loading,data,errorMsg} = useRequest<IAddress[]>("./addressList.json")//獲取對(duì)象數(shù)據(jù) watch(data, () => { if (data.value) { console.log(data.value.length) } }) return { loading, data, errorMsg } } }); </script>
測(cè)試數(shù)據(jù)有對(duì)象類(lèi)型的 address.json:
{ "name": "家", "address": "開(kāi)發(fā)區(qū)人民路22號(hào)", "distance": "100km" }
還有數(shù)組類(lèi)型的 addressList.json
關(guān)于“Vue怎么自定義hook函數(shù)”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí),可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會(huì)為大家更新不同的知識(shí)點(diǎn)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。