Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VueJS Syntax: Running method on mount

I would like to load some data with vue-resource when a page loads, then re-load that data if a refresh button is pressed.

To keep my code DRY I wanted to put this functionality in a method. This was my first attempt:

index.html:

<div id="app"></div>

app.js:

const Vue = window.Vue = require("vue");
require("vue-resource");
const App = require("./components/App.vue");

window.app = new Vue({
    el: "#app",
    render: h => h(App)
});

components/app.vue:

<template>
    <div>
        <h1>Test</h1>
        <p>{text}</p>
        <button @click="loadData">Reload</button>
    </div>
</template>
<script>
export default {
    // This fails
    mounted: this.loadData,
    methods: {
        loadData() {
            // This syntax may be wrong, too. But the function isn't
            // even running, so I haven't started to debug this yet
            this.$http.get("https://icanhazip.com")
                .then(xhr => this.text = xhr.body);
        }
    }
};
</script>

This throws an error on line 10 of components/app.vue:

    mounted: this.loadData,

Saying Uncaught TypeError: Cannot read property 'reloadData' of undefined

How can I get the mounted function to refer to any of the methods defined in methods?

like image 267
stevendesu Avatar asked Sep 26 '17 13:09

stevendesu


People also ask

What is mounted () in Vue?

The mounted() hook is the most commonly used lifecycle hook in Vue. Vue calls the mounted() hook when your component is added to the DOM. It is most often used to send an HTTP request to fetch data that the component will then render.

What is created () in Vue JS?

Created is generally used for fetching data from backend API and setting it to data properties.

How do you mount in Vue?

Mounted is the most-often used hook in the lifecycle. mounted() is called after DOM has been mounted so you can access the reactive component, templates, and DOM elements and manipulate them. In Server Side Rendering created()is used over mounted() because mounted() is not present in it.

What is $El in Vue?

The $el option in Vue provides Vue with an existing HTML element to mount the Vue instance generated using the new keyword. this. $el. querySelector is used to access HTML elements and modify the element's properties.


2 Answers

You should use mounted event in following way with correct method declaration.

export default {        
    mounted() {
      this.loadData();
    },
    methods: {
        loadData() {
            // This syntax may be wrong, too. But the function isn't
            // even running, so I haven't started to debug this yet
            this.$http.get("https://icanhazip.com")
                .then(xhr => this.text = xhr.body);
        }
    }
};

More details can be found here.
https://v2.vuejs.org/v2/api/#mounted

like image 166
Thusitha Avatar answered Oct 19 '22 20:10

Thusitha


You need to use the v-on ( @ ) directive to listen to DOM events like click and run some function in methods in this way:

<button @click="loadData">Reload</button>

@Thusitha is correct for the mounted, you need to update it.

like image 30
Kumar_14 Avatar answered Oct 19 '22 18:10

Kumar_14