如何等待 axios 调用完成
How to wait for an axios call to complete
在 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
调用 returns 导出对象,使所有设置为空。
如何在导出常量之前等待调用?
你撑不住export
,但是你能做的就是这样,就是调用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) => {
...
})
在 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
调用 returns 导出对象,使所有设置为空。
如何在导出常量之前等待调用?
你撑不住export
,但是你能做的就是这样,就是调用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) => {
...
})