U Vue.js projektu composables koriste se funkcije za ponovnu upotrebu logike i stanja između različitih komponenti. Evo nekih popularnih Vue.js composables koje možete koristiti u svom projektu:
useLocalStorage i useSessionStorage
Oni composables vam pomažu pohraniti i upravljati podacima u lokalnom pregledniku storage ili session storage pregledniku.
<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 i useThrottle
Oni composables vam omogućuju da primijenite debounce ili gas na funkcije rukovanja događajima, pomažući u kontroli učestalosti izvršavanja akcije.
<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
To composable vam pomaže u praćenju medijskih upita za izvođenje odgovarajućih radnji na temelju veličina zaslona.
<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
To composable vam pomaže upravljati asinkronim zadacima i pratiti njihov status(na čekanju, uspjeh, pogreška).
<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
To composable vam pomaže u praćenju događaja na DOM elementima i izvršavanju odgovarajućih radnji.
<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
To composable vam pomaže pristupiti router informacijama i parametrima URL upita u Vue Router aplikaciji.
<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
To composable vam pomaže u upravljanju prikazom paginiranih podataka i radnjama navigacije.
<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
To composable vam pomaže pratiti sjecište elementa s viewport, što je korisno za izvođenje radnji kada element postane vidljiv ili nestane.
<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
To composable vam pomaže kopirati podatke u clipboard i upravljati stanjem kopiranja.
<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
To composable vam pomaže upravljati stanjem URL upita i ažurirati sadržaj stranice na temelju URL upita.
<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>
Imajte na umu da za korištenje ovih composables morate instalirati @vueuse/core
biblioteku koristeći npm ili yarn. Oni composables vam pomažu da ponovno upotrijebite uobičajenu logiku i stanje u svom Vue.js projektu, optimizirajući razvojni proces i upravljanje kodom.