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

javascript - Declaring dynamic data in a component, Vuejs 2 - Stack Overflow

programmeradmin6浏览0评论

I'm begining now with vue js 2. I have this code, which receives dynamic data from the server (laravel 5.3 app), the problem is when i try to declare the users array on the ponent instead of declaring in the Vue() instance (in this case works great):

HTML:

<div id="app">
    <my-ponent>
        <ul id="users-list">
            <li v-for="user in users">{{user.name}}</li>
        </ul>
    </my-ponent>
</div>

JS:

Vueponent('my-ponent', {
    template: '#users-list',
    data: function () {
        return {
            users: []
        }
    },
    mounted: function() {
        this.$http.get('http://127.0.0.1:8003/api/test').then((response) => {
            console.log(response.body);
            this.users = response.body;
        }, function() {
            alert('brrh');
        });
    }
});
new Vue({
    el: '#app',
});

The error message: "Uncaught ReferenceError: users is not defined"

I'm begining now with vue js 2. I have this code, which receives dynamic data from the server (laravel 5.3 app), the problem is when i try to declare the users array on the ponent instead of declaring in the Vue() instance (in this case works great):

HTML:

<div id="app">
    <my-ponent>
        <ul id="users-list">
            <li v-for="user in users">{{user.name}}</li>
        </ul>
    </my-ponent>
</div>

JS:

Vue.ponent('my-ponent', {
    template: '#users-list',
    data: function () {
        return {
            users: []
        }
    },
    mounted: function() {
        this.$http.get('http://127.0.0.1:8003/api/test').then((response) => {
            console.log(response.body);
            this.users = response.body;
        }, function() {
            alert('brrh');
        });
    }
});
new Vue({
    el: '#app',
});

The error message: "Uncaught ReferenceError: users is not defined"

Share Improve this question edited Oct 14, 2016 at 14:51 IronAces 1,8931 gold badge28 silver badges37 bronze badges asked Oct 14, 2016 at 14:30 MiguelMiguel 1,9076 gold badges21 silver badges31 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

Looks like you're trying to do an inline-template try adding that directive to your <my-ponent> call.

    <div id="app">
            <my-ponent inline-template>
                    <ul id="users-list">
                            <li v-for="user in users">{{user.name}}</li>
                    </ul>
            </my-ponent>
    </div>

You will also have to remove the template parameter from your ponent call

    Vue.ponent('my-ponent', {
        data: function () {
            return {
                users: []
            }
        },
        mounted: function() {
            this.$http.get('http://127.0.0.1:8003/api/test').then((response) => {
                console.log(response.body);
                this.users = response.body;
            }, function() {
                alert('brrh');
            });
        }
    });

    new Vue({
            el: '#app',
    });

jsFiddle

发布评论

评论列表(0)

  1. 暂无评论