有什么方法可以显示来自api的数据?

时间:2019-11-02 14:21:28

标签: laravel vuejs2

我正在构建一个spa,需要从控制器中获取数据,而从API中获取数据,但是无法在vue控制器中显示数据。

JSON.stringify(array)

我正在从api获取数据,但无法将该数据保存在ids对象中。如何做到这一点。

我已经尝试过 this.ids = response.data 但它不起作用。

我想在id对象中获取用户详细信息,例如用户名,名称,电子邮件等。还是有其他好的方法可以做到这一点。 这是我的控制器文件

   export default {
 data() {
    return {
      ids:{},
        form: new Form({
          name:'',
          relation:'',
          gender:'',
          dob:'',
          marriage_status:'',
          matrimonial:'',
          email:'',
          mobile:'',
          father_name:'',
          mother_name:'',
          address:'',
          city:'',
          state:'',
          occupation:'',
          department:'',
          post_graduate_degree:'',
          post_graduate_university:'',
          post_graduate_university_city:'',
          post_graduate_university_state:'',
          post_graduate_university_percentage:'',
          post_graduate_year_of_passing:'',
          graduate_degree:'',
          graduate_university:'',
          graduate_university_city:'',
          graduate_university_state:'',
          graduate_university_percentage:'',
          graduate_year_of_passing:'',
          class_12_board : '',
          class_12_school_name:'',
          class_12_percentage:'',
          class_12_year_of_passing:'',
          class_12_city:'',
          class_10_board:'',
          class_10_school_name:'',
          class_10_percentage:'',
          class_10_year_of_passing:'',
          class_10_city:'',
          username:'',
          password:''

          })
        }
      },
    methods:{
  loaduser(){

   axios.get('api/userDetails').then(function (response) {
// handle success
 this.ids = response.data;
    console.log(response.data);
  console.log(this.ids);
  })
   },
      created() {
      this.loaduser();
   }
    }

1 个答案:

答案 0 :(得分:0)

您错过的一件事是“ this”。您正在将数据分配给局部变量。在这里,我更新了代码

export default {
  data() {
    return {
      ids:{},
      //
    },
  methods:{
    loaduser(){
      axios.get('api/userDetails').then(function (response) {
        // handle success
        this.ids = response.data;
        console.log(response.data);
        console.log(this.ids);
      })
    },
    //
  }
}