#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.
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.
DEV Community
#Vue
I want share a Data abc which is string in my one component to another component .Any Idea How to...
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.. π¦ π¦
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.. π¦ π¦
DEV Community
Digital Clock using Vue 3 Composition API
Hello Readers, In this blog post we will see how can we create a digital clock using Vue 3...
Forwarded from ΠΠ½ΠΈΠ³ΠΈ Π΄Π»Ρ ΠΏΡΠΎΠ³ΡΠ°ΠΌΠΌΠΈΡΡΠΎΠ²
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, Π²Ρ Π½Π΅ ΡΠΎΠ»ΡΠΊΠΎ Π½Π°ΡΡΠΈΡΠ΅ΡΡ ΡΠΎΠ·Π΄Π°Π²Π°ΡΡ ΠΏΡΠΈΠ»ΠΎΠΆΠ΅Π½ΠΈΡ, Π½ΠΎ ΠΈ ΠΏΠΎΠΉΠΌΠ΅ΡΠ΅, ΠΊΠ°ΠΊ ΡΡΡΠ΅ΠΊΡΠΈΠ²Π½ΠΎ ΡΠ΅ΡΠ°ΡΡ ΠΎΠ±ΡΠΈΠ΅ ΠΏΡΠΎΠ±Π»Π΅ΠΌΡ, ΠΏΡΠΈΠΌΠ΅Π½ΡΡ ΡΡΡΠ΅ΡΡΠ²ΡΡΡΠΈΠ΅ ΠΏΠ°ΡΡΠ΅ΡΠ½Ρ ΠΏΡΠΎΠ΅ΠΊΡΠΈΡΠΎΠ²Π°Π½ΠΈΡ. ΠΠ±Π»Π°Π΄Π°Ρ ΡΡΠΈΠΌΠΈ Π·Π½Π°Π½ΠΈΡΠΌΠΈ, Π²Ρ ΡΠΌΠΎΠΆΠ΅ΡΠ΅ Π½Π΅ ΠΈΠ·ΠΎΠ±ΡΠ΅ΡΠ°ΡΡ Π²Π΅Π»ΠΎΡΠΈΠΏΠ΅Π΄ Π΄Π»Ρ ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ ΠΏΡΠΎΠ΅ΠΊΡΠ°, Π° ΡΠΊΠΎΠ½ΠΎΠΌΡ Π²ΡΠ΅ΠΌΡ ΡΠΎΠ·Π΄Π°Π²Π°ΡΡ ΠΏΡΠΎΠ³ΡΠ°ΠΌΠΌΠ½ΠΎΠ΅ ΠΎΠ±Π΅ΡΠΏΠ΅ΡΠ΅Π½ΠΈΠ΅, Π°Π΄Π°ΠΏΡΠΈΡΡΠ΅ΠΌΠΎΠ΅ ΠΊ Π±ΡΠ΄ΡΡΠΈΠΌ ΠΈΠ·ΠΌΠ΅Π½Π΅Π½ΠΈΡΠΌ.
Π‘ΠΊΠ°ΡΠ°ΡΡ ΠΊΠ½ΠΈΠ³Ρ
ΠΠ²ΡΠΎΡ: Olaf Zander
ΠΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΡΡΡΠ°Π½ΠΈΡ: 296
#vue
Π ΡΡΠΎΠΉ ΠΊΠ½ΠΈΠ³Π΅ Vue 3 ΡΠ°ΡΡΠΌΠ°ΡΡΠΈΠ²Π°Π΅ΡΡΡ Ρ ΡΠ°ΠΌΡΡ ΠΎΡΠ½ΠΎΠ², Π²ΠΊΠ»ΡΡΠ°Ρ ΠΊΠΎΠΌΠΏΠΎΠ½Π΅Π½ΡΡ ΠΈ Π΄ΠΈΡΠ΅ΠΊΡΠΈΠ²Ρ, ΠΈ ΠΏΠΎΡΡΠ΅ΠΏΠ΅Π½Π½ΠΎ ΠΏΠ΅ΡΠ΅Ρ ΠΎΠ΄ΠΈΡ ΠΊ Π±ΠΎΠ»Π΅Π΅ ΠΏΡΠΎΠ΄Π²ΠΈΠ½ΡΡΡΠΌ ΡΠ΅ΠΌΠ°ΠΌ, ΡΠ°ΠΊΠΈΠΌ ΠΊΠ°ΠΊ ΠΌΠ°ΡΡΡΡΡΠΈΠ·Π°ΡΠΈΡ, ΡΠΏΡΠ°Π²Π»Π΅Π½ΠΈΠ΅ ΡΠΎΡΡΠΎΡΠ½ΠΈΠ΅ΠΌ, Π²Π΅Π±-ΡΠ°Π±ΠΎΡΠΈΠ΅ ΠΈ Π°Π²ΡΠΎΠ½ΠΎΠΌΠ½ΠΎΠ΅ Ρ ΡΠ°Π½Π΅Π½ΠΈΠ΅. ΠΠ°ΡΠ°Π² Ρ ΠΏΡΠΎΡΡΠΎΠΉ ΡΡΡΠ°Π½ΠΈΡΡ, Π²Ρ ΠΏΠΎΡΡΠ΅ΠΏΠ΅Π½Π½ΠΎ ΡΠΎΠ·Π΄Π°Π΄ΠΈΡΠ΅ ΠΏΠΎΠ»Π½ΠΎΡΡΠ½ΠΊΡΠΈΠΎΠ½Π°Π»ΡΠ½ΠΎΠ΅ ΠΌΠ½ΠΎΠ³ΠΎΠΏΠΎΡΠΎΡΠ½ΠΎΠ΅, Π°Π²ΡΠΎΠ½ΠΎΠΌΠ½ΠΎΠ΅ ΠΈ ΡΡΡΠ°Π½Π°Π²Π»ΠΈΠ²Π°Π΅ΠΌΠΎΠ΅ ΠΏΡΠΎΠ³ΡΠ΅ΡΡΠΈΠ²Π½ΠΎΠ΅ Π²Π΅Π±-ΠΏΡΠΈΠ»ΠΎΠΆΠ΅Π½ΠΈΠ΅.
Π ΡΠΎΠΌΡ Π²ΡΠ΅ΠΌΠ΅Π½ΠΈ, ΠΊΠΎΠ³Π΄Π° Π²Ρ Π·Π°ΠΊΠΎΠ½ΡΠΈΡΠ΅ ΡΠΈΡΠ°ΡΡ ΡΡΡ ΠΊΠ½ΠΈΠ³Ρ ΠΏΠΎ Vue, Π²Ρ Π½Π΅ ΡΠΎΠ»ΡΠΊΠΎ Π½Π°ΡΡΠΈΡΠ΅ΡΡ ΡΠΎΠ·Π΄Π°Π²Π°ΡΡ ΠΏΡΠΈΠ»ΠΎΠΆΠ΅Π½ΠΈΡ, Π½ΠΎ ΠΈ ΠΏΠΎΠΉΠΌΠ΅ΡΠ΅, ΠΊΠ°ΠΊ ΡΡΡΠ΅ΠΊΡΠΈΠ²Π½ΠΎ ΡΠ΅ΡΠ°ΡΡ ΠΎΠ±ΡΠΈΠ΅ ΠΏΡΠΎΠ±Π»Π΅ΠΌΡ, ΠΏΡΠΈΠΌΠ΅Π½ΡΡ ΡΡΡΠ΅ΡΡΠ²ΡΡΡΠΈΠ΅ ΠΏΠ°ΡΡΠ΅ΡΠ½Ρ ΠΏΡΠΎΠ΅ΠΊΡΠΈΡΠΎΠ²Π°Π½ΠΈΡ. ΠΠ±Π»Π°Π΄Π°Ρ ΡΡΠΈΠΌΠΈ Π·Π½Π°Π½ΠΈΡΠΌΠΈ, Π²Ρ ΡΠΌΠΎΠΆΠ΅ΡΠ΅ Π½Π΅ ΠΈΠ·ΠΎΠ±ΡΠ΅ΡΠ°ΡΡ Π²Π΅Π»ΠΎΡΠΈΠΏΠ΅Π΄ Π΄Π»Ρ ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ ΠΏΡΠΎΠ΅ΠΊΡΠ°, Π° ΡΠΊΠΎΠ½ΠΎΠΌΡ Π²ΡΠ΅ΠΌΡ ΡΠΎΠ·Π΄Π°Π²Π°ΡΡ ΠΏΡΠΎΠ³ΡΠ°ΠΌΠΌΠ½ΠΎΠ΅ ΠΎΠ±Π΅ΡΠΏΠ΅ΡΠ΅Π½ΠΈΠ΅, Π°Π΄Π°ΠΏΡΠΈΡΡΠ΅ΠΌΠΎΠ΅ ΠΊ Π±ΡΠ΄ΡΡΠΈΠΌ ΠΈΠ·ΠΌΠ΅Π½Π΅Π½ΠΈΡΠΌ.
Π‘ΠΊΠ°ΡΠ°ΡΡ ΠΊΠ½ΠΈΠ³Ρ