【发布时间】:2020-01-11 00:33:14
【问题描述】:
我有一个最后一部分是动态的 api: src/api/user.js
export function getProfileData() {
return request({
url: 'http://localhost:8000/profile_update/DYNAMIC USER ID HERE',
method: 'get',
})
}
我在这个动作中调用这个 api: 存储/模块/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 调用? 希望我已经清楚了。 我已经提取了很多代码,所以如果需要我会全部发布。
【问题讨论】:
标签: vuejs2 axios vue-router store dynamic-url