I dtionscadal Vue.js, composables is feidhmeanna iad a úsáidtear chun loighic a athúsáid agus a lua idir comhpháirteanna éagsúla. Seo roinnt Vue.js composables a bhfuil an-tóir orthu is féidir leat a úsáid i do thionscadal:
useLocalStorage agus useSessionStorage
Cuidíonn siad seo composables leat sonraí a stóráil agus a bhainistiú sa bhrabhsálaí áitiúil storage nó session storage sa bhrabhsálaí.
<template>
<div>
<p>Last visited: {{ lastVisited }}</p>
</div>
</template>
<script>
import { useLocalStorage } from '@vueuse/core';
export default {
setup() {
const lastVisited = useLocalStorage('last_visited', new Date());
return {
lastVisited,
};
},
};
</script>
useDebounce agus useThrottle
Ligeann siad seo composables duit debounce nó throttle a chur i bhfeidhm ar fheidhmeanna láimhseála imeachtaí, ag cabhrú le minicíocht cur i gcrích na gníomhaíochta a rialú.
<template>
<div>
<input v-model="searchQuery" @input="handleSearch" />
<p>Search results: {{ searchResults }}</p>
</div>
</template>
<script>
import { ref } from 'vue';
import { useDebounce } from '@vueuse/core';
export default {
setup() {
const searchQuery = ref('');
const searchResults = ref([]);
const handleSearch = useDebounce(() => {
// Perform search based on searchQuery
// Update searchResults
}, 300);
return {
searchQuery,
searchResults,
handleSearch,
};
},
};
</script>
useMediaQueries
Cuidíonn sé seo composable leat ceisteanna ó na meáin a rianú chun gníomhartha sofhreagracha a dhéanamh bunaithe ar mhéideanna scáileáin.
<template>
<div>
<p>Current screen size: {{ screenSize }}</p>
</div>
</template>
<script>
import { useMediaQueries } from '@vueuse/core';
export default {
setup() {
const { screenSize } = useMediaQueries({
mobile: 'screen and(max-width: 640px)',
tablet: 'screen and(max-width: 1024px)',
desktop: 'screen and(min-width: 1025px)',
});
return {
screenSize,
};
},
};
</script>
useAsync
Cuidíonn sé seo composable leat tascanna asincrónacha a bhainistiú agus monatóireacht a dhéanamh ar a stádas(ar feitheamh, rath, earráid).
<template>
<div>
<button @click="fetchData">Fetch Data</button>
<p v-if="status === 'pending'">Loading...</p>
<p v-if="status === 'success'">Data loaded: {{ data }}</p>
<p v-if="status === 'error'">Error loading data.</p>
</div>
</template>
<script>
import { ref } from 'vue';
import { useAsync } from '@vueuse/core';
export default {
setup() {
const fetchData = async() => {
// Simulate fetching data
const response = await fetch('https://api.example.com/data');
const data = await response.json();
return data;
};
const { execute, value: data, status } = useAsync(fetchData);
return {
fetchData: execute,
data,
status,
};
},
};
</script>
useEventListener
Cuidíonn sé seo composable leat imeachtaí ar eilimintí DOM a rianú agus gníomhartha comhfhreagracha a dhéanamh.
<template>
<div>
<p>Mouse position: {{ mouseX }}, {{ mouseY }}</p>
</div>
</template>
<script>
import { ref } from 'vue';
import { useEventListener } from '@vueuse/core';
export default {
setup() {
const mouseX = ref(0);
const mouseY = ref(0);
useEventListener('mousemove',(event) => {
mouseX.value = event.clientX;
mouseY.value = event.clientY;
});
return {
mouseX,
mouseY,
};
},
};
</script>
useRouter
Cuidíonn sé seo composable leat rochtain a fháil ar router fhaisnéis agus paraiméadair iarratais URL in Vue Router fheidhmchlár.
<template>
<div>
<p>Current route: {{ currentRoute }}</p>
</div>
</template>
<script>
import { useRoute } from 'vue-router';
export default {
setup() {
const route = useRoute();
const currentRoute = route.fullPath;
return {
currentRoute,
};
},
};
</script>
usePagination
Cabhraíonn sé seo composable leat gníomhartha taispeána sonraí uimhrithe agus loingseoireachta a bhainistiú.
<template>
<div>
<ul>
<li v-for="item in currentPageData":key="item.id">{{ item.name }}</li>
</ul>
<button @click="previousPage":disabled="currentPage === 1">Previous</button>
<button @click="nextPage":disabled="currentPage === totalPages">Next</button>
</div>
</template>
<script>
import { ref, computed } from 'vue';
import { usePagination } from '@vueuse/core';
export default {
setup() {
const data = ref([...]); // Paginated data
const itemsPerPage = 10;
const currentPage = ref(1);
const { currentPageData, nextPage, previousPage, totalPages } = usePagination(data, itemsPerPage, currentPage);
return {
currentPageData,
nextPage,
previousPage,
currentPage,
totalPages,
};
},
};
</script>
useIntersectionObserver
Cabhraíonn sé seo composable leat trasnú eilimint a rianú leis an viewport, atá úsáideach chun gníomhartha a dhéanamh nuair a éiríonn eilimint infheicthe nó nuair a imíonn sí.
<template>
<div>
<div ref="observedElement">Observed Element</div>
<p v-if="isIntersecting">Element is intersecting!</p>
</div>
</template>
<script>
import { ref } from 'vue';
import { useIntersectionObserver } from '@vueuse/core';
export default {
setup() {
const observedElement = ref(null);
const { isIntersecting } = useIntersectionObserver(observedElement, {});
return {
observedElement,
isIntersecting,
};
},
};
</script>
useClipboard
Cuidíonn sé seo composable leat sonraí a chóipeáil chuig an clipboard stát cóipeála agus a bhainistiú.
<template>
<div>
<p>Text to copy: {{ textToCopy }}</p>
<button @click="copyText">Copy Text</button>
<p v-if="copied">Copied to clipboard!</p>
</div>
</template>
<script>
import { ref } from 'vue';
import { useClipboard } from '@vueuse/core';
export default {
setup() {
const textToCopy = ref('Hello, Vue.js!');
const { copy, copied } = useClipboard();
const copyText =() => {
copy(textToCopy.value);
};
return {
textToCopy,
copyText,
copied,
};
},
};
</script>
useRouteQuery
Cuidíonn sé seo composable leat staid iarratais URL a bhainistiú agus ábhar an leathanaigh a nuashonrú bunaithe ar fhiosruithe URL.
<template>
<div>
<input v-model="searchQuery" />
<button @click="updateQuery">Search</button>
<p>Search results: {{ searchResults }}</p>
</div>
</template>
<script>
import { ref } from 'vue';
import { useRouter, useRouteQuery } from '@vueuse/core';
export default {
setup() {
const router = useRouter();
const searchQuery = ref('');
const { search } = useRouteQuery();
const searchResults = ref([]);
const updateQuery =() => {
router.push({ query: { search: searchQuery.value } });
// Perform search based on searchQuery and update searchResults
};
return {
searchQuery,
searchResults,
updateQuery,
};
},
};
</script>
Tabhair faoi deara, le do thoil, chun iad seo a úsáid composables, go gcaithfidh tú an @vueuse/core
leabharlann a shuiteáil ag baint úsáide as npm nó snáth. Cuidíonn siad seo composables leat an loighic choiteann a athúsáid agus a lua i do thionscadal Vue.js, ag barrfheabhsú an phróisis forbartha agus an bhainistíocht cóid.