Update Digital Clock Without Refreshing Page In Vuejs

admin_img Posted By Bajarangi soft , Posted On 16-09-2020

Current date and time updates automatically by Refreshing or Reloading a Page in Vuejs and laravel

digital_clock

Let's learn

1.Install new laravel project to implement code. https://laravel.com/docs/5.8/installation

composer create-project --prefer-dist laravel/laravel digital_clock
 run above command in command prompt.

2.Install node.js package for digital_clock project . click here to download it.

3.Install npm package for digital_clock project.click here to see the documentation.Install the dependencies in the local node_modules folder.

npm install
 run above command in terminal.

4.Install moment.js package for digital_clock project. this package help us to display time and date https://momentjs.com/
npm install moment --save

 run above command in terminal.

5.Open resources/js/app.js in digital_clock project you can see this below code in that file.
Vue.component('example-component', require('./components/ExampleComponent.vue').default);


6.Open resources/views/welcome.blade.php in digital_clock project and implement below code.
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Vuejs auto reload digital clock</title>
    <link href="{{asset('css/app.css')}}" rel="stylesheet">
    <meta name="csrf-token" content="{{csrf_token()}}"/>
    <!-- Fonts -->
    <!-- Styles -->
    <style>
        html, body {
            color: white;
            font-family: 'Nunito', sans-serif;
            font-weight: 200;
            height: 100vh;
            margin: 0;
            background-image: url({{asset("image/background.png")}});//save this image in any folder
        background-repeat: no-repeat;
            background-size: cover;
            background-position: bottom;
        }
        .title-title{
            font-size: 70px;
        }
        .position-ref {
            position: relative;
        }

        .top-right {
            position: absolute;
            right: 10px;
            top: 18px;
        }

        .content {
            text-align: center;
        }

        .title {
            font-size: 84px;
        }

        .links > a {
            color: #636b6f;
            padding: 0 25px;
            font-size: 13px;
            font-weight: 600;
            letter-spacing: .1rem;
            text-decoration: none;
            text-transform: uppercase;
        }

        .m-b-md {
            margin-bottom: 30px;
        }
    </style>
</head>
<body >
<div id="app">//id which is declared in app.js file
    <example-component></example-component>//view the example-component file
</div>
<script src="{{asset('js/app.js')}}"></script>
</body>
</html>

7.Open resources/js/components/ExampleComponent.vue file in digital_clock project to implement below code.
<template>
    <div class="container">
        <div id="wrapper" >
            <br>
            <p class="title-title text-center" >Vuejs auto reload digital clock </p>
            <h1 class="text-black text-center">{{message}}</h1>
            <p class="title-title  is-3  text-center" v-text="currentdate"></p>
            <p class="title-title  is-3  text-center " v-text="currentday"></p>
            <p class="title-title is-3 text-center " v-text="currentTime"></p>
        </div>
    </div>
</template>

<script>
    var moment = require('moment');
    export default {
        mounted() {
            console.log(' Sample component!!!!!!.')//check this output  in console
        },
        data(){
            return{
                name:"admin",
                message: 'Current Date and Time:',
                currentTime: null,
                currentdate:null,
                currentday:null,
            }

        },
        methods: {
            updateCurrentTime() {
                this.currentTime = moment().format('LTS');
                this.currentdate =  moment().format('DD-MM-YYYY ');
                this.currentday =  moment().format(' dddd');
            }
        },
        created() {
            this.currentTime = moment().format('LTS');
            this.currentdate =  moment().format('DD-MM-YYYY');
            this.currentday =  moment().format(' dddd');
            setInterval(() => this.updateCurrentTime(), 1 * 1000);
        }
        // display date and time  without refreshing page using  vuejs and laravel
    }
</script>

8.Run command npm run watch.

npm run dev  active and  updates only when you run command.
npm run watch does the same, but then it stays active and "watches" for updates.vue and .js files. If it detects a change so you can just refresh the page.

Related Post