vue3-HOOKS模塊化處理方式
vue3模塊化處理
vue3版本的更新,就是能搞更好的重用機制,可以把想要得模塊獨立出去
eg:顯示一個當(dāng)前時間的工能,在多個頁面需要調(diào)用的時候不用重復(fù)的調(diào)用
可以在src目錄下,新建一個文件夾hooks(所有抽離出來的功能模塊都可以放到這個文件夾里),
然后再新建一個文件useNowTime.js,這里使用use開頭是一個使用習(xí)慣,代表是一個抽離出來的模塊
import { ref } from "vue"; const nowTime = ref("00:00:00"); const getNowTime = () => { ? ? const now = new Date(); ? ? const hour = now.getHours() < 10 ? "0" + now.getHours() : now.getHours(); ? ? const minu = ? ? ? ? now.getMinutes() < 10 ? "0" + now.getMinutes() : now.getMinutes(); ? ? const sec = ? ? ? ? now.getSeconds() < 10 ? "0" + now.getSeconds() : now.getSeconds(); ? ? nowTime.value = hour + ":" + minu + ":" + sec; ? ? setTimeout(getNowTime, 1000); }; export { nowTime, getNowTime }
注意:需要將定義的變量nowTime和方法getNowTime通過export導(dǎo)出
- 使用的時候跟在setup中定義的變量和方法一樣使用
- 使用模塊化封裝一個遠程調(diào)用接口的組件
建立useURLAxios.js文件
在文件中定義遠程加載需要的 變量和axios請求
import {ref} from 'vue' import axios from 'axios'; function usURLAxios(url) { ? ? const result = ref(null) ? ? const loading = ref(true) ? ? const loaded =ref(false) ? ? const error =ref(null) ? ? axios.get(url).then((res)=>{ ? ? ? ? loading.value = false ? ? ? ? loaded.value = true ? ? ? ? result.value = res.data ? ? }).catch(e=>{ ? ? ? ? error.value = e ? ? ? ? loading.value = false ? ? }) ? ? return { ? ? ? ? result, ? ? ? ? loading, ? ? ? ? loaded, ? ? ? ? error ? ? } } export default usURLAxios
使用時
新增一個.vue文件
<template> ? <div> ? ? <button @click="getImg">隨機展示圖片</button> ? ? <div v-if="thisloading">Loading.....</div> ? ? <img v-if="thisloaded" :src="thisresult.message" /> ? ? <div></div> ? </div> </template>
<script> import { reactive, toRefs } from "vue"; import useUrlAxios from "../hooks/useURLAxios"; export default { ? setup() { ? ? const data = reactive({ ? ? ? thisresult: null, ? ? ? thisloading: true, ? ? ? thisloaded: false, ? ? ? getImg: () => { ? ? ? ? const { result, loading, loaded } = useUrlAxios( ? ? ? ? ? "https://dog.ceo/api/breeds/image/random" ? ? ? ? ); ? ? ? ? data.thisresult = result; ? ? ? ? data.thisloading = loading; ? ? ? ? data.thisloaded = loaded; ? ? ? ? console.log( ? ? ? ? ? 22222, ? ? ? ? ? data.thisresult, ? ? ? ? ? data.thisloading, ? ? ? ? ? data.thisloaded, ? ? ? ? ? result, ? ? ? ? ? loaded, ? ? ? ? ? loading ? ? ? ? ); ? ? ? }, ? ? }); ? ? const refData = toRefs(data); ? ? return { ...refData }; ? }, }; </script> <style lang="scss" scoped> </style>
vue hooks理解與使用
vue的hooks和mixins功能相似,但又比mixins具有以下幾個優(yōu)勢:
- 允許hooks間相互傳遞值
- 組件之間重用狀態(tài)邏輯
- 明確指出邏輯來自哪里
demo源碼示意
hook1:
import { useData, useMounted } from 'vue-hooks'; export function windowwidth() { const data = useData({ width: 0 }) useMounted(() => { data.width = window.innerWidth }) // this is something we can consume with the other hook return { data } }
import { useData, useMounted } from 'vue-hooks'; export function windowwidth() { const data = useData({ width: 0 }) useMounted(() => { data.width = window.innerWidth }) // this is something we can consume with the other hook return { data } }
hook2:
// the data comes from the other hook export function logolettering(data) { useMounted(function () { // this is the width that we stored in data from the previous hook if (data.data.width > 1200) { // we can use refs if they are called in the useMounted hook const logoname = this.$refs.logoname; Splitting({ target: logoname, by: "chars" }); TweenMax.staggerFromTo(".char", 5, { opacity: 0, transformOrigin: "50% 50% -30px", cycle: { color: ["red", "purple", "teal"], rotationY(i) { return i * 50 } } }, ...
在組件內(nèi)部,我們可以將 hook1 作為參數(shù)傳遞給 hook2:
<script> import { logolettering } from "./../hooks/logolettering.js"; import { windowwidth } from "./../hooks/windowwidth.js"; ? export default { ? hooks() { ? ? logolettering(windowwidth()); ? } }; </script>
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue router動態(tài)路由設(shè)置參數(shù)可選問題
這篇文章主要介紹了vue-router動態(tài)路由設(shè)置參數(shù)可選,文中給大家提到了vue-router 動態(tài)添加 路由的方法,需要的朋友可以參考下2019-08-08Vue?Router4路由導(dǎo)航守衛(wèi)實例全面解析
這篇文章主要為大家介紹了Vue?Router4路由導(dǎo)航守衛(wèi)實例全面解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11vue項目網(wǎng)頁自適應(yīng)等比例放大縮小實例代碼
等比例縮放可以在不同的分辨率下都能夠一屏展示,不會有滾動條的問題,也不會有適配問題,下面這篇文章主要給大家介紹了關(guān)于vue項目網(wǎng)頁自適應(yīng)等比例放大縮小的相關(guān)資料,需要的朋友可以參考下2022-11-11laravel5.3 vue 實現(xiàn)收藏夾功能實例詳解
這篇文章主要介紹了laravel5.3 vue 實現(xiàn)收藏夾功能,本文通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下2018-01-01vue3點擊出現(xiàn)彈窗后背景變暗且不可操作的實現(xiàn)代碼
這篇文章主要介紹了vue3點擊出現(xiàn)彈窗后背景變暗且不可操作的實現(xiàn)代碼,本文通過實例代碼圖文相結(jié)合給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-08-08vue+element UI中如何給指定日期添加標(biāo)記
這篇文章主要介紹了vue+element UI中如何給指定日期添加標(biāo)記問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02axios的interceptors多次執(zhí)行問題解決
這篇文章主要為大家介紹了axios中interceptors多次執(zhí)行問題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-06-06