我有一个api,其最后一部分是动态的: src / api / user.js
export function getProfileData() {
return request({
url: 'http://localhost:8000/profile_update/DYNAMIC USER ID HERE',
method: 'get',
})
}
在此操作中,我正在调用此api: store / modules / user.js
setProfileData({ commit }) {
return getProfileData(userId).then(response => {
console.log('RESPONSE setProfileData action dispatch', response)
commit('SET_PROFILE_DATA', response.results)
})
(在此模块中,我还具有状态和突变-工作) 此操作在此处调度: src / views / users-list
goToUserProfile(user) {
const userId = user.id
this.$store.dispatch('user/setCurrentUser').then(() => {
const profileData = this.$store.dispatch('user/setProfileData(userId)').then(() => {
const profileData = this.$store.getters.profileData
this.profileData = profileData
console.log('users-list sending profile data: ', profileData )
this.$router.push({
name: 'SeeUserProfile',
params: {
userId: userId
}
})
}).catch((err) => {
console.log('ERROR IN fetchin' userDataProfile: ', err)
this.loading = false
})
})
}
自变量用户来自
@click.prevent="goToUserProfile(data.item), selectUser(data.item)"
所以data.item =单个所选用户
此代码不起作用。 我需要在api中使用动态用户ID。 在商店中,我还有:
setCurrentUser({ commit }, userData) {
commit('SET_CURRENT_USER', userData)}
工作正常-在userData内有属性ID。
我尝试使用 / $ {params.userId} ,但是如何在api中传递呢?
如何将userId传递给axios调用? 希望我已经清楚了。 我已经提取了很多代码,因此如果需要,我将全部发布。
答案 0 :(得分:1)
getProfileData
函数getProfileData(usedId)
添加id参数并将其包含在url中:
url: http://localhost:8000/profile_update/${ DYNAMIC USER ID HERE }
(请记住在链接中加上反引号)setProfileData
函数添加id参数:setProfileData({ commit }, userId)
this.$route.params.userId
希望这会有所帮助。
使用整个URL的反引号代替双引号,这样就可以了。