close

How to call a vue.js function on page load

Hello Guys, How are you all? Hope You all Are Fine. Today we are going to learn How to call a vue.js function on page load in javascript. So Here I am Explain to you all the possible methods here.

Without wasting your time, Let’s start This Article.

How to call a vue.js function on page load ?

  1. How to call a vue.js function on page load ?

    to call a vue.js function on page load You need to do something like this (If you want to call the method on page load):
    new Vue({ // ... methods:{ getUnits: function() {...} }, created: function(){ this.getUnits() } });

  2. How to call a vue.js function on page load ?

    to call a vue.js function on page load You need to do something like this (If you want to call the method on page load):
    new Vue({ // ... methods:{ getUnits: function() {...} }, created: function(){ this.getUnits() } });

Method 1

You need to do something like this (If you want to call the method on page load):

new Vue({
    // ...
    methods:{
        getUnits: function() {...}
    },
    created: function(){
        this.getUnits()
    }
});

Method 2

If you get data in array you can do like below. It’s worked for me

    <template>
    {{ id }}
    </template>
    <script>

    import axios from "axios";

        export default {
            name: 'HelloWorld',
            data () {
                return {
                    id: "",

                }
            },
    mounted() {
                axios({ method: "GET", "url": "https://localhost:42/api/getdata" }).then(result => {
                    console.log(result.data[0].LoginId);
                    this.id = result.data[0].LoginId;
                }, error => {
                    console.error(error);
                });
            },
</script>

Summery

It’s all About this article. Hope all method helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which method worked for you? Thank You.

Leave a Comment