vue3-HOOKS模塊化處理方式
vue3模塊化處理
vue3版本的更新,就是能搞更好的重用機(jī)制,可以把想要得模塊獨立出去
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中定義的變量和方法一樣使用
- 使用模塊化封裝一個遠(yuǎn)程調(diào)用接口的組件
建立useURLAxios.js文件
在文件中定義遠(yuǎn)程加載需要的 變量和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">隨機(jī)展示圖片</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)文章
Vue3中使用ref標(biāo)簽對組件進(jìn)行操作方法
這篇文章主要介紹了Vue3中使用ref標(biāo)簽對組件進(jìn)行操作方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-04-04
vue3?keep-alive實現(xiàn)tab頁面緩存功能
如何在我們切換tab標(biāo)簽的時候,緩存標(biāo)簽最后操作的內(nèi)容,簡單來說就是每個標(biāo)簽頁中設(shè)置的比如搜索條件及結(jié)果、分頁、新增、編輯等數(shù)據(jù)在切換回來的時候還能保持原樣,這篇文章介紹vue3?keep-alive實現(xiàn)tab頁面緩存功能,感興趣的朋友一起看看吧2023-04-04
Vue中使用vue-plugin-hiprint插件進(jìn)行打印的功能實現(xiàn)
hiprint 是一個web 打印的js組件,無需安裝軟件,支持windows,macOS,linux 系統(tǒng),支持移動端,PC端瀏覽器,angular,vue,react 等 分頁預(yù)覽,打印,操作簡單,運行快速,本文介紹了Vue中使用vue-plugin-hiprint插件進(jìn)行打印,需要的朋友可以參考下2025-04-04
在移動端使用vue-router和keep-alive的方法示例
這篇文章主要介紹了在移動端使用vue-router和keep-alive的方法示例,vue-router與keep-alive提供的路由體驗與移動端是有一定差別的,感興趣的小伙伴們可以參考一下2018-12-12

