Vue.js Digest πŸ‡·πŸ‡Ί πŸ‡ΊπŸ‡Έ
39 subscribers
389 photos
745 links
ДайдТСст новостСй ΠΈΠ· ΠΌΠΈΡ€Π° vuejs
Download Telegram
#Vue

https://dev.to/rahulmishra117/vue-50ho
I want share a Data abc which is string in my one component to another component .Any Idea How to share that data.
Digital Clock using Vue 3 Composition API

https://dev.to/snehalk/digital-clock-using-vue-3-composition-api-5cmc
Hello Readers,
In this blog post we will see how can we create a digital clock using Vue 3 composition API. Composition API is a new feature added in Vue through which we can reuse the code in multiple vue component.
For more details about how to use composition API you can refer my previous blog. I providing a link - go through it for basic information about it.








What is Composition API in Vue 3
Snehal ・ Dec 23 '21 ・ 3 min read

#javascript
#codenewbie
#vue
#webdev




So let's start with main topic.
First we need to create a component named as DigitalClock.vue



<template>
<div class="flex h-screen">
<div class="w-full lg:w-1/4 m-auto p-7 shadow-lg shadow-pink-400 border-4 border-t-purple-600 border-r-pink-600 border-b-pink-600 border-l-indigo-600 bg-gradient-to-r from-indigo-500 via-purple-500 to-pink-500">
<!-- <p class="font-bold text-white text-lg">{{ currentTime.toLocaleString() }}</p> -->
<p class="font-bold text-white pt-3 text-6xl">{{ currentTime.toLocaleTimeString() }}</p>
<p class="font-bold text-white text-sm mb-1 flex justify-end mr-3">{{ currentTime.toLocaleDateString() }}</p>
</div>
</div>
</template>
<script>
import { useCurrentTime } from "../composables/useCurrentTime";
export default {
name: "CurrentTimeExample",
setup() {
const { currentTime } = useCurrentTime();
console.log(currentTime.value);
return { currentTime };
},
};
</script>



In the above code we are calling useCurrentTime method from the useCurrentTime.js file where we are going to write our main logic using composition api and from that we will call a currentTime and return its value to the component.
To create a composition api's we will create a folder named as composables where we keep/create all composition api's.
As stated above create a folder named as composables in src folder and create js file as useCurrentTime.js. (src/composables/useCurrentTime.js)



import { ref, onBeforeUnmount } from "vue";
export const useCurrentTime = () => {
const currentTime = ref(new Date());
const updateCurrentTime = () => {
currentTime.value = new Date();
};
const updateTimeInterval = setInterval(updateCurrentTime, 1000);
onBeforeUnmount(() => {
clearInterval(updateTimeInterval);
});
return {
currentTime,
};
};


In above code we have created a const variable as currentTime which holds current Date and Time, and a method updateCurrentTime to update the current time. There is an another method called as updateTimeInterval which will update the time after given set of interval.

You can see a hook called as onBeforeUnmount() which will clear the currentTime when component is unmounted.

And the last thing is we are returning the current time, so wherever we have used/called this useCurrenttTime.js we will have the value.
In this way wherever we require the date n time we can reuse this code by simply importing it to the component.
You can also refer live demo in sandbox.



Happy Reading.. πŸ¦„ 🦁
Vue.js 3 Design Patterns and Best Practices: Develop scalable and robust applications with Vite, Pinia, and Vue Router (2023)
Автор: Olaf Zander
ΠšΠΎΠ»ΠΈΡ‡Π΅ΡΡ‚Π²ΠΎ страниц: 296

#vue

Π’ этой ΠΊΠ½ΠΈΠ³Π΅ Vue 3 рассматриваСтся с самых основ, Π²ΠΊΠ»ΡŽΡ‡Π°Ρ ΠΊΠΎΠΌΠΏΠΎΠ½Π΅Π½Ρ‚Ρ‹ ΠΈ Π΄ΠΈΡ€Π΅ΠΊΡ‚ΠΈΠ²Ρ‹, ΠΈ постСпСнно ΠΏΠ΅Ρ€Π΅Ρ…ΠΎΠ΄ΠΈΡ‚ ΠΊ Π±ΠΎΠ»Π΅Π΅ ΠΏΡ€ΠΎΠ΄Π²ΠΈΠ½ΡƒΡ‚Ρ‹ΠΌ Ρ‚Π΅ΠΌΠ°ΠΌ, Ρ‚Π°ΠΊΠΈΠΌ ΠΊΠ°ΠΊ ΠΌΠ°Ρ€ΡˆΡ€ΡƒΡ‚ΠΈΠ·Π°Ρ†ΠΈΡ, ΡƒΠΏΡ€Π°Π²Π»Π΅Π½ΠΈΠ΅ состояниСм, Π²Π΅Π±-Ρ€Π°Π±ΠΎΡ‡ΠΈΠ΅ ΠΈ Π°Π²Ρ‚ΠΎΠ½ΠΎΠΌΠ½ΠΎΠ΅ Ρ…Ρ€Π°Π½Π΅Π½ΠΈΠ΅. Начав с простой страницы, Π²Ρ‹ постСпСнно создадитС ΠΏΠΎΠ»Π½ΠΎΡ„ΡƒΠ½ΠΊΡ†ΠΈΠΎΠ½Π°Π»ΡŒΠ½ΠΎΠ΅ ΠΌΠ½ΠΎΠ³ΠΎΠΏΠΎΡ‚ΠΎΡ‡Π½ΠΎΠ΅, Π°Π²Ρ‚ΠΎΠ½ΠΎΠΌΠ½ΠΎΠ΅ ΠΈ устанавливаСмоС прогрСссивноС Π²Π΅Π±-ΠΏΡ€ΠΈΠ»ΠΎΠΆΠ΅Π½ΠΈΠ΅.

К Ρ‚ΠΎΠΌΡƒ Π²Ρ€Π΅ΠΌΠ΅Π½ΠΈ, ΠΊΠΎΠ³Π΄Π° Π²Ρ‹ Π·Π°ΠΊΠΎΠ½Ρ‡ΠΈΡ‚Π΅ Ρ‡ΠΈΡ‚Π°Ρ‚ΡŒ эту ΠΊΠ½ΠΈΠ³Ρƒ ΠΏΠΎ Vue, Π²Ρ‹ Π½Π΅ Ρ‚ΠΎΠ»ΡŒΠΊΠΎ Π½Π°ΡƒΡ‡ΠΈΡ‚Π΅ΡΡŒ ΡΠΎΠ·Π΄Π°Π²Π°Ρ‚ΡŒ прилоТСния, Π½ΠΎ ΠΈ ΠΏΠΎΠΉΠΌΠ΅Ρ‚Π΅, ΠΊΠ°ΠΊ эффСктивно Ρ€Π΅ΡˆΠ°Ρ‚ΡŒ ΠΎΠ±Ρ‰ΠΈΠ΅ ΠΏΡ€ΠΎΠ±Π»Π΅ΠΌΡ‹, примСняя ΡΡƒΡ‰Π΅ΡΡ‚Π²ΡƒΡŽΡ‰ΠΈΠ΅ ΠΏΠ°Ρ‚Ρ‚Π΅Ρ€Π½Ρ‹ проСктирования. Обладая этими знаниями, Π²Ρ‹ смоТСтС Π½Π΅ ΠΈΠ·ΠΎΠ±Ρ€Π΅Ρ‚Π°Ρ‚ΡŒ вСлосипСд для ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ ΠΏΡ€ΠΎΠ΅ΠΊΡ‚Π°, Π° экономя врСмя ΡΠΎΠ·Π΄Π°Π²Π°Ρ‚ΡŒ ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ½ΠΎΠ΅ обСспСчСниС, Π°Π΄Π°ΠΏΡ‚ΠΈΡ€ΡƒΠ΅ΠΌΠΎΠ΅ ΠΊ Π±ΡƒΠ΄ΡƒΡ‰ΠΈΠΌ измСнСниям.

Π‘ΠΊΠ°Ρ‡Π°Ρ‚ΡŒ ΠΊΠ½ΠΈΠ³Ρƒ