PostDetailView.vue 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <script setup lang="ts">
  2. import { router } from '@/router'
  3. import { onMounted, ref } from 'vue'
  4. import { api } from '@/utils/axios.ts'
  5. import type { PostDetail } from '@/models'
  6. import PostDetailContent from '@/components/PostDetailContent.vue'
  7. function parseId() {
  8. let targetPostId = router.currentRoute.value.params['id']
  9. if (Array.isArray(targetPostId)) {
  10. if (targetPostId.length) {
  11. targetPostId = targetPostId[0]
  12. } else return undefined
  13. }
  14. const id = Number(targetPostId)
  15. if (isNaN(id)) {
  16. return undefined
  17. }
  18. return id
  19. }
  20. let postId = NaN
  21. const postDetail = ref<PostDetail | null>(null)
  22. async function freshData() {
  23. const res = await api.postGet(postId)
  24. if (res.code == 200) {
  25. postDetail.value = res.data
  26. return true
  27. } else return false
  28. }
  29. onMounted(() => {
  30. postId = parseId() ?? NaN
  31. if (!postId) {
  32. router.push('/')
  33. }
  34. freshData().then((res) => {
  35. if (!res) router.push('/')
  36. })
  37. })
  38. </script>
  39. <template>
  40. <PostDetailContent v-if="postDetail" :target="postDetail" />
  41. </template>
  42. <style scoped></style>