vue3-HOOKS模塊化處理方式
vue3模塊化處理
vue3版本的更新,就是能搞更好的重用機制,可以把想要得模塊獨立出去
eg:顯示一個當前時間的工能,在多個頁面需要調(diào)用的時候不用重復的調(diào)用
可以在src目錄下,新建一個文件夾hooks(所有抽離出來的功能模塊都可以放到這個文件夾里),
然后再新建一個文件useNowTime.js,這里使用use開頭是一個使用習慣,代表是一個抽離出來的模塊
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導出
- 使用的時候跟在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)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
vue router動態(tài)路由設置參數(shù)可選問題
這篇文章主要介紹了vue-router動態(tài)路由設置參數(shù)可選,文中給大家提到了vue-router 動態(tài)添加 路由的方法,需要的朋友可以參考下2019-08-08
laravel5.3 vue 實現(xiàn)收藏夾功能實例詳解
這篇文章主要介紹了laravel5.3 vue 實現(xiàn)收藏夾功能,本文通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下2018-01-01
vue3點擊出現(xiàn)彈窗后背景變暗且不可操作的實現(xiàn)代碼
這篇文章主要介紹了vue3點擊出現(xiàn)彈窗后背景變暗且不可操作的實現(xiàn)代碼,本文通過實例代碼圖文相結合給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-08-08
axios的interceptors多次執(zhí)行問題解決
這篇文章主要為大家介紹了axios中interceptors多次執(zhí)行問題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-06-06

