I'm currently learning Vue.js 2. I encountered an issue with my code while receiving dynamic data from a server (using Laravel 5.3). The problem arises when I attempt to declare the users array within the component instead of declaring it in the Vue() instance, where it works perfectly:
HTML:
<div id="app">
<my-component>
<ul id="users-list">
<li v-for="user in users">{{user.name}}</li>
</ul>
</my-component>
</div>
JS:
Vue.component('my-component', {
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',
});
An error message is shown as: "Uncaught ReferenceError: users is not defined"