【问题标题】:How to wait for an axios call to complete如何等待 axios 调用完成
【发布时间】:2018-05-29 13:00:15
【问题描述】:

在一个 vuejs 应用程序中,我试图从 ajax 调用的数据中初始化一个对象:

let settings = {}
api.readConfig().then((config) => {
  settings = {
    userStore: new Oidc.WebStorageStateStore(),
    authority: config.data.urls.auth,
    client_id: config.data.clientid,
    redirect_uri: `${window.location.protocol}//${window.location.hostname}${window.location.port ? `:${window.location.port}` : ''}${process.env.ROUTER_BASE}static/callback.html`,
    response_type: 'id_token token',
    post_logout_redirect_uri: config.data.urls.auth,
  }
})

const authMgr = new Oidc.UserManager(settings)
export default authMgr

对象由调用返回导出,使所有设置为空。

在导出我的 const 之前如何等待通话?

【问题讨论】:

    标签: javascript ajax vue.js axios


    【解决方案1】:

    export 是你撑不下去的,但你能做的是这样的事情,那就是调用promise 链。在这里你可以将解决promise的责任转移给callee模块。

    export default api.readConfig().then((config) => {
      return {
        userStore: new Oidc.WebStorageStateStore(),
        authority: config.data.urls.auth,
        client_id: config.data.clientid,
        redirect_uri: `${window.location.protocol}//${window.location.hostname}${window.location.port ? `:${window.location.port}` : ''}${process.env.ROUTER_BASE}static/callback.html`,
        response_type: 'id_token token',
        post_logout_redirect_uri: config.data.urls.auth,
      }
    }).then((settings) => {
      return new Oidc.UserManager(settings);
    })
    

    然后在你的 callee 模块上你可以做这样的事情。

    var config = require('./config');
    config().then((userManager) => {
       ...
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-15
      • 2020-06-08
      • 1970-01-01
      • 1970-01-01
      • 2019-11-19
      • 1970-01-01
      • 1970-01-01
      • 2018-06-09
      相关资源
      最近更新 更多