如何在init上使用没有声明密钥数据的vue js

时间:2017-10-11 09:32:14

标签: javascript jquery vuejs2

我使用jquery与api rest进行通信数据。并使用vue js将数据呈现给我的模板,但是我有错误。

[Vue warn]: Error in render function: "TypeError: Cannot read property 'first_name' of undefined"

这是我的代码:

var dashboard = new Vue({
  el: '#dashboard',
  data:{
    profile: {}
  }
});
axios.get(url_profile, { headers: {"Authorization": "JWT " + token} })
    .then(function(res){
        dashboard.$data.profile = res.data;
    })
    .catch(function(err){
        toastr.err(err);
    });

感谢。

1 个答案:

答案 0 :(得分:3)

https://vuejs.org/v2/guide/instance.html#Data-and-Methods

  

如果您知道以后需要某个属性,但它开始为空或不存在,则需要设置一些初始值。例如:

data: {
  newTodoText: '',
  visitCount: 0,
  hideCompletedTodos: false,
  todos: [],
  error: null
}