运行 在 vue3 中调用函数后计算
Run computed after call function in vue3
我使用 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
}
}
})
我不确定你想要完成什么。但是你需要初始化你的计算 属性.
改变
ProfileData = computed(() => store.state.profile.ProfileData)
至
const ProfileData = computed(() => store.state.profile.ProfileData)
已解决
我在 template
中使用了 watch
和 v-if
并且省略了 async/await
我使用 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
}
}
})
我不确定你想要完成什么。但是你需要初始化你的计算 属性.
改变
ProfileData = computed(() => store.state.profile.ProfileData)
至
const ProfileData = computed(() => store.state.profile.ProfileData)
已解决
我在 template
中使用了 watch
和 v-if
并且省略了 async/await