在axios中设置预加载器

时间:2019-08-20 12:37:40

标签: javascript axios

当请求URL时:/ api / v1 / todos我设置了预加载器。函数todo ()todos ()函数内部被调用。在todo ()函数内部,请求/ api / v1 / todos / $ {todoId}也被调用。发生跳跃。预加载器出现,消失。然后出现。如何设置预加载器仅显示在请求/ api / v1 / todos / $ {todoId}上,而不显示在/ api / v1 / todos /上。

axios.interceptors.request.use(function(config) {
  if (config.url === `/api/v1/todos`) {
    document.body.classList.add('preloader');
}

componentDidMOunt() {
    this.todos();
}

todos = (idGroupTasks) => {
    axios({
        url: `/api/v1/todos`,
        method: "GET"
    })
    .then(res => { 
        this.setState({
            todos: res.data
        });
        this.todo(res.data[0].id)
    })
    .catch(error => {
        console.log(error);
    }) 


}

todo = (idTodo) => {
    axios({
        url: `/api/v1/todo/{idTodo}`,
        method: "GET"
    })
    .then(res => { 
        this.setState({
            todos: res.data
        });
    })
    .catch(error => {
        console.log(error);
    }) 
}

0 个答案:

没有答案