Vue.js Axios can';t集数据

Vue.js Axios can';t集数据,vue.js,axios,Vue.js,Axios,以下是我的数据: data: function(){ return { contas: [{id: 3, nome: "Conta de telefone", pago: false, valor: 55.99, vencimento: "22/08/2016"}] //debug test value }; }, 下面是我的get请求: beforeMount()

以下是我的数据:

data: function(){
    return {
        contas: [{id: 3,
            nome: "Conta de telefone",
            pago: false,
            valor: 55.99,
            vencimento: "22/08/2016"}] //debug test value
    };
},
下面是我的get请求:

beforeMount() {
    axios.get('http://127.0.0.1/api/bills')
        .then(function (response) {
            console.log("before: " + this.contas);
            this.contas = response.data;
            console.log("after: " + this.contas);
        });
},
问题是我无法从
axios.get()
中访问
this.contas
。我尝试了
Vue.set(这是'contas',response.data)
window.listaPagarComponent.contas=response.data没有成功

我的控制台显示:

before: undefined
after: [object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
但Vue Devtools仅显示:

contas: Array[1]
  0: Object
    id: 3
    nome: "Conta de telefone"
    pago: false
    valor: 55.99
    vencimento: "22/08/2016"

这是我的。

在诸如
数据和
创建的
等选项函数中,vue为我们将
绑定到视图模型实例,因此我们可以使用
this.contas
,但在
then
内的函数中,
未绑定

因此您需要保留视图模型,如(
创建的
意味着组件的数据结构已组装,这就足够了,
安装的
将更延迟操作):

如果您只希望支持现代浏览器(或使用babel等transpiler),则可以在ES6标准中使用箭头功能,如:


内部箭头函数是根据词法上下文绑定的,这意味着上面代码段中的
与创建的
中的相同,这是我们想要的。

要能够访问axios内的this.contas.get()是否需要绑定“this”以保持变量使用范围:

mounted() {
    axios.get('http://127.0.0.1/api/bills')
     .then(function (response) {
        console.log("before: " + this.contas);
        this.contas = response.data;
        console.log("after: " + this.contas);
     }.bind(this));
}

尝试使用
created()
hook代替
beforeMount()
。如果您已经在contas数组中定义了一些数据,那么我认为您应该执行array.push。好的,您可以在数据模型中创建新数组,并为其设置响应数据吗?然后签出,项目是否存储在数组中。不幸的是,我不使用Axios,我宁愿使用Vue资源。@Belmin没有任何更改。。。这只是一个调试测试值。我不想要这个值。问题是我不能使用
this.contas
引用组件的数据
contas
。没有任何功能可以工作。我认为axios是一个“对象”,所以当我使用
this
时,它指的是axios。是的,已经用字符串尝试过了。字符串
var test=''
。然后什么都没变。我认为这是指axios。Vue资源不适用于Vue.js 2不确定,抱歉,我说我从未使用过Axios。Vue资源适用于Vue 2。我在许多项目中都使用过它。请看这个万岁!这也解决了我的问题。。。谢谢不管我学了多少次并理解了“this”,它总是会给我带来问题。该死的,非常感谢,因为我需要“this”语法的实例
created() {
    axios.get('http://127.0.0.1/api/bills')
        .then((response) => {
                this.contas = response.data;
                });
}
mounted() {
    axios.get('http://127.0.0.1/api/bills')
     .then(function (response) {
        console.log("before: " + this.contas);
        this.contas = response.data;
        console.log("after: " + this.contas);
     }.bind(this));
}