Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用Axios从GitLab API获取所有数据?_Javascript_Vue.js_Axios_Gitlab Api - Fatal编程技术网

Javascript 如何使用Axios从GitLab API获取所有数据?

Javascript 如何使用Axios从GitLab API获取所有数据?,javascript,vue.js,axios,gitlab-api,Javascript,Vue.js,Axios,Gitlab Api,我想从GitLab实例托管的所有项目中获取数据。现在我知道GitLab默认显示20个项目,所以我需要设置每页显示的更多实例: https://gitlab-repo.com/api/v4/projects?per_page=100 因此,它会导致相应的axios代码: const url='https://gitlab-repo.com/api/v4/projects?per_page=100' const vm = new Vue({ el: "#app", data: { resu

我想从GitLab实例托管的所有项目中获取数据。现在我知道GitLab默认显示20个项目,所以我需要设置每页显示的更多实例:

https://gitlab-repo.com/api/v4/projects?per_page=100
因此,它会导致相应的axios代码:

const url='https://gitlab-repo.com/api/v4/projects?per_page=100'
const vm = new Vue({
el: "#app",
data: {
    results: [],
    loading: true,
    errored: false
},
mounted() {
    axios
        .get(url)
        .then(response => (this.results = response.data))
        .catch(error => {
            console.log(error)
            this.errored = true
        })
        .finally(() => this.loading = false)
}
}) 
问题是,GitLab每页最多可以显示100个项目,而我有更多的项目

我曾考虑在axios的get函数的
params
部分对实例(项目)的数量设置一个条件,但不知道如何编写


我如何在axios请求中指出,如果项目数大于100,我希望加载下一页上的数据

您首先需要编写一个函数来请求特定的页面,如下所示:

function getPage(page) {
  let url = `https://gitlab-repo.com/api/v4/projects?per_page=100&page=${page}`;
  return axios
        .get(url)
        .then(response => (response.data))
}
如果您的环境中有
async/await
,我会这样做,不断请求下一页,直到得到<100个结果

let resultCount = 100;
let results = [];
let page = 1;
while (resultCount === 100) {
  let newResults = await getPage(page);
  page++;
  resultCount = newResults.length;
  results = results.concat(newResults);
}

你从哪里得到每页的
我在文档中没有看到它?您必须使用分页,获得总页数,并要求第2页、第3页、第4页等。。。gitlab中的per_页面受到限制,以防止服务器加载太多数据和崩溃。@Dominic我在SF上发现了它,当时我不知道为什么只显示了20个项目:谢谢你的回答。从您所写的内容中,我了解到第二段代码是Vue应用程序的
mounted
属性背后的主要思想,对吗?@avazula抱歉,我对Vue或mounted属性一无所知。不用担心。谢谢。