【发布时间】:2017-06-09 09:38:56
【问题描述】:
我正在使用 Vuex 显示来自“store.js”的用户列表。那个js文件有这样的数组。
var store = new Vuex.Store({
state: {
customers: [
{ id: '1', name: 'user 1',},
]
}
})
我想在同一个数组中插入一组新值
{ id: '1', name: 'user 1',}
上面的值是从一个 URL (vue-resource) 获得的。下面是将获取的数据推送到数组的代码。但是,数据没有插入
mounted: function() {
this.$http.get('http://localhost/facebook-login/api/get_customers.php')
.then(response => {
return response.data;
})
.then(data => {
store.state.customers.push(data) // not working!!
console.log(data) // prints { id: '2', name: 'User 2',}
store.state.customers.push({ id: '2', name: 'User 2',})
});
}
【问题讨论】:
-
get_customers.php返回一个数组还是单个用户? -
这样放行吗? store.state.customers.push({data})
-
@rogeriolino 实际上它应该返回整个用户数组。但是返回单用户也足够了。我都在尝试。他们都没有工作
-
@Miguel 试过了。不工作
标签: javascript arrays vue.js vuejs2 vue-resource