获取所有用户并使用vue js显示他们 [英] Fetch all users and display them with vue js
本文介绍了获取所有用户并使用vue js显示他们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对Vue js还是很陌生,现在我正尝试通过ajax调用获取用户,从而将它们输出到表中.我得到的用户没有问题,但是当我尝试用新数据设置data.user时,我收到一条错误消息,提示未定义用户属性或方法. 这是我的用户列表组件:
I am very new to Vue js and I am now trying to output all the users in a table by fetching them with an ajax call. I get the users with no issue but then when I try to set the data.user with the new data I get an error saying the property or method users is not defined. This is my user list components:
<template>
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">List of users</div>
<div class="panel-body">
<table class="table">
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr v-for="user in users">
<td>user.name</td>
<td>user.lastaname</td>
<td>user.email</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</template>
<script>
export default {
data: function () {
return {
users: []
}
},
mounted() {
axios.get('/users')
.then(function (response) {
console.log(response.data);
})
.catch(function (error) {
console.log(error.message);
});
},
}
</script>
推荐答案
尝试一下
<script>
export default {
data: function () {
return {
users: []
}
},
methods: {
getUsers: function() {
var app = this;
axios.get('/users')
.then(function (response) {
app.users = response.data;
})
.catch(function (error) {
console.log(error.message);
});
}
},
created() {
this.getUsers();
},
}
ES6语法
getUsers() {
axios.get('/users')
.then((response) => this.users = response.data)
.catch((error) => console.log(error.message))
}
这篇关于获取所有用户并使用vue js显示他们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文