简体   繁体   English

Vue.js应用程序中的预加载器?

[英]Preloader in Vue.js application?

I am new in Vue.js ecosystem and need some advice. 我是Vue.js生态系统的新手 ,需要一些建议。

I make GET request by axios package. 我通过axios包发出GET request I want to show preloader for full page until all the data came. 我希望在所有数据出现之前显示整页预加载器。 I think this task is not new but I want to understand how such things are usually done in Vue.js. 我认为这个任务并不新鲜,但我想了解这些事情通常是如何在Vue.js中完成的。

For preloader I create new component. 对于预加载器,我创建了新组件。 I am little bit confused. 我有点困惑。 How to call that component from other one and make visable at a certain time. 如何从其他组件调用该组件并在特定时间使其可见。

You could show your preload ui before an axios all & spread (to make make all your requests) and then to hide that preload ui. 你可以在axios allspread之前展示你的preload ui(以满足你的所有请求), then隐藏那个preload ui。 Here is in example: 这是例子:

// show preload ui
showSpinnerAnimation();

// Requests will be executed in parallel...
axios.all([
  axios.get('https://somelink');
  axios.get('https://someotherlink')
])
.then(axios.spread(function (somelinkResponse, someotherlinkResponse) {
  //... but this callback will be executed only when both requests are complete.
  console.log('somelinkResponse', somelinkResponse.data);
  console.log('someotherlinkResponse', someotherlinkResponse.data);

  // hide preload ui
  hideSpinnerAnimation();
}));

Axios work differently then ajax. Axios的工作方式与ajax不同。 You can say the advance version of javascript ajax function. 你可以说javascript ajax函数的高级版本。

Small Example of axios: axios的小例子:

 const axios = require('axios');

// Make a request for a user with a given ID
axios.get('/user?ID=12345')
  .then(function (response) {
    // handle success
    console.log(response);
  })
  .catch(function (error) {
    // handle error
    console.log(error);
  })
  .then(function () {
    // always executed
  });

// Optionally the request above could also be done as
axios.get('/user', {
    params: {
      ID: 12345
    }
  })
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  })
  .then(function () {
    // always executed
  });  

// Want to use async/await? Add the `async` keyword to your outer function/method.
async function getUser() {
  try {
    const response = await axios.get('/user?ID=12345');
    console.log(response);
  } catch (error) {
    console.error(error);
  }
}

Reference: https://github.com/axios/axios 参考: https//github.com/axios/axios

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM