vue js data()中的字段未通过second(help)函数的呼叫来初始化
我几乎没有Vue JS的经验。
我有2个函数loadComponentsofuser()
和loaduserid()
,因此LoadComponentsofuser()使用UserID字段,必须由LoadUserID()函数加载。
data() {
return {
userId: ''
}
},
created() {
this.loadComponentsOfUser()
},
methods(): {
loadUserId() {
axios.get('getUserId').then(res => {
this.userId = res.data
}).catch(() => {
...
})
});
},
loadComponentsOfUser() {
this.loadUserId()
axios.get('users/' + this.userId).then(res => {
}).catch(() => {
...
})
});
}
loadUserId()函数正常工作并从服务器加载正确的值
,但是当调用LoadComponentSofuser()时,This.userid字段看起来似乎尚未初始化,并且一个空字段出现在AXIOS上。
我的问题是,为什么在调用LoadUserId()之后没有初始化该字段?
I don't have almost any experience with vue js.
I have 2 functions loadComponentsOfUser()
and loadUserId()
, so that loadComponentsOfUser() uses the userID field, which must be loaded by the loadUserId() function.
data() {
return {
userId: ''
}
},
created() {
this.loadComponentsOfUser()
},
methods(): {
loadUserId() {
axios.get('getUserId').then(res => {
this.userId = res.data
}).catch(() => {
...
})
});
},
loadComponentsOfUser() {
this.loadUserId()
axios.get('users/' + this.userId).then(res => {
}).catch(() => {
...
})
});
}
The loadUserId() function works correctly and loads the correct value from the server
But when loadComponentsOfUser() is called, the this.userId field looks like it has not been initialized and an empty field comes to the axios.
my question is why the field was not initialized after calling loadUserId()?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要等待响应,您可以使用
async
和等待
:You need to wait for responses, yo can use
async
andawait
: