最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - VueJS Syntax: Running method on mount - Stack Overflow

programmeradmin3浏览0评论

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("")
                .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?

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?

Share Improve this question edited Oct 25, 2019 at 14:01 stevendesu asked Sep 26, 2017 at 13:15 stevendesustevendesu 16.8k22 gold badges117 silver badges207 bronze badges 1
  • 1 BTW: vue-resource isn't really a thing anymore. medium.com/the-vue-point/retiring-vue-resource-871a82880af4 – sandrooco Commented Sep 26, 2017 at 13:37
Add a comment  | 

2 Answers 2

Reset to default 18

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

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.

发布评论

评论列表(0)

  1. 暂无评论