index.vue 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <template>
  2. <n-dropdown
  3. trigger="hover"
  4. @select="handleSelect"
  5. :show-arrow="true"
  6. :options="options"
  7. >
  8. <n-button quaternary circle>
  9. <n-icon size="20" :depth="1">
  10. <PersonOutlineIcon v-show="fallback" />
  11. </n-icon>
  12. <n-avatar
  13. v-show="!fallback"
  14. round
  15. size="small"
  16. :src="imageUrl"
  17. @error="errorHandle"
  18. />
  19. </n-button>
  20. </n-dropdown>
  21. </template>
  22. <script lang="ts" setup>
  23. import { h, ref } from 'vue'
  24. import { NAvatar, NText } from 'naive-ui'
  25. import { renderIcon } from '@/utils/index'
  26. import { openDoc, logout } from '@/utils/page'
  27. import {
  28. Person as PersonOutlineIcon,
  29. LogOutOutline as LogOutOutlineIcon,
  30. DocumentText as DocumentTextIcon
  31. } from '@vicons/ionicons5'
  32. const imageUrl = 'https://www.naiveui.com/assets/naivelogo.93278402.svg'
  33. // 是否失败
  34. const fallback = ref(false)
  35. // 用户图标渲染
  36. const renderUserInfo = () => {
  37. return h(
  38. 'div',
  39. {
  40. style: 'display: flex; align-items: center; padding: 8px 12px;'
  41. },
  42. [
  43. h(NAvatar, {
  44. round: true,
  45. style: 'margin-right: 12px;',
  46. src: imageUrl
  47. }),
  48. h('div', null, [
  49. h('div', null, [
  50. h(NText, { depth: 2 }, { default: () => '奔跑的面条' })
  51. ])
  52. ])
  53. ]
  54. )
  55. }
  56. const options = [
  57. {
  58. label: '我的信息',
  59. key: 'info',
  60. type: 'render',
  61. render: renderUserInfo
  62. },
  63. {
  64. type: 'divider',
  65. key: 'd1'
  66. },
  67. {
  68. label: '说明文档',
  69. key: 'doc',
  70. icon: renderIcon(DocumentTextIcon)
  71. },
  72. {
  73. type: 'divider',
  74. key: 'd2'
  75. },
  76. {
  77. label: '退出登录',
  78. key: 'logout',
  79. icon: renderIcon(LogOutOutlineIcon)
  80. }
  81. ]
  82. // 图片渲染错误
  83. const errorHandle = (e: Event) => {
  84. fallback.value = true
  85. }
  86. const handleSelect = (key: string) => {
  87. switch (key) {
  88. case 'doc':
  89. openDoc()
  90. break
  91. case 'logout':
  92. logout()
  93. break
  94. }
  95. }
  96. </script>