如何使用vueJS在for循环中运行GET请求?

时间:2018-10-25 14:25:05

标签: javascript vue.js axios

我想填充来自vuejs应用程序的GET REST调用。

当前我有以下模板

using (WebClient client = new WebClient())
{
    client.Encoding = Encoding.UTF8;
    client.Headers["Content-Type"] = "application/json";
    client.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(credentials));
    string myDataResult = client.UploadString(url, "POST", myQuery);

    <result handling code>
}

通常,我可以通过以下方式运行GET请求

<template>
<div class="app-container">
  <div class="currentjob">
    <h1>Current Jobs</h1>
    <table>
      <tr>
        <th>ID</th>
        <th>Status</th>
        <th>AlgorithmID</th>
        <th>Result</th>
      </tr>
      <tr v-for="job in jobs">
        <td>{{ job.id }}</td>
        <td>{{ job.status }}</td>
        <td>{{ job.algorithmId }}</td>
        <td>{{ getResult(job.id) }}</td>
      </tr>
    </table>
  </div>
</div>
</template>

<script>
    export default
    {
        name: 'CurrentJobs',
        data()
        {
            return {
                jobs: null
            }
        },
        mounted()
        {
            this.$axios
                .get(this.$baseUrl + 'job')
                .then(response => (this.jobs = response.data))
        },
        methods:
        {
            getResult: function(jobid)
            {
                  this.$axios.get(this.$baseUrl + 'job/result/'+jobid).
                    then(response =>
                      {
                       return response.data // does not work 
                      }) 
            }
        }
    }
</script>

但是我无法在this.$axios. get(this.$baseUrl + 'job/result/'+jobid). then(response => { // return response.data // does not work } ) 方法内的arrow函数中添加return语句。如何返回REST调用的响应?我缺少什么基本概念?

2 个答案:

答案 0 :(得分:0)

尝试一下:

this.$axios.
get(this.$baseUrl + 'job/result/'+jobid).
then(response =>
{
 this.jobs = response.data 
}
) 

这样做,您会将响应传递到您的jobs数据

答案 1 :(得分:0)

致电时返回您的axios结果。

示例:

getResult: function(jobid)
     {
       return this.$axios.get(this.$baseUrl + 'job/result/'+jobid).
        then(response =>
        {
            return response.data
        })
     }