在ve3中调用函数后运行computed



我使用vue3,我想在vuex中设置数据并在调用API后运行计算。但不幸的是,计算在getProfile函数之前运行。我使用async-await,但它不工作(我使用console.log,但我得到它undefined)。

import { defineComponent, ref, computed, reactive, getCurrentInstance } from 'vue'
import { useStore } from 'vuex'
export default defineComponent({
setup() {
const internalInstance = getCurrentInstance()
const axios = internalInstance.appContext.config.globalProperties.axios
const searchDropdown = ref(false)
let ProfileData = reactive({})
const store = useStore()
const showSearchDropdown = () => {
searchDropdown.value = true
}
const hideSearchDropdown = () => {
searchDropdown.value = false
}
const getProfile = () => {
axios.get('users/profile/')
.then(profile => {
store.dispatch('profile/setProfile', profile.data)
})
}
getProfile()
ProfileData = computed(() => store.state.profile.ProfileData)

console.log(ProfileData.value)
return {
searchDropdown,
showSearchDropdown,
hideSearchDropdown,
getProfile,
ProfileData

}
}
})

我不知道你想做什么。但是你需要初始化你的computed属性

改变
ProfileData = computed(() => store.state.profile.ProfileData)

const ProfileData = computed(() => store.state.profile.ProfileData)

解决我在template中使用watchv-if,我省略了async/await

最新更新