效果
image.png

3-1 创建avatar组件

src/layout/components/avatar/index.vue

  1. <template>
  2. <el-dropdown
  3. class="avatar-container">
  4. <div class="avatar-wrapper">
  5. <img :src="avatar" class="user-avatar">
  6. <i class="el-icon-caret-bottom" />
  7. </div>
  8. <template #dropdown>
  9. <el-dropdown-menu>
  10. <router-link to="/">
  11. <el-dropdown-item>首页</el-dropdown-item>
  12. </router-link>
  13. <router-link to="/profile/index">
  14. <el-dropdown-item>个人设置</el-dropdown-item>
  15. </router-link>
  16. <el-dropdown-item divided @click="logout">
  17. <span style="display: block">退出登录</span>
  18. </el-dropdown-item>
  19. </el-dropdown-menu>
  20. </template>
  21. </el-dropdown>
  22. </template>
  23. <script lang="ts">
  24. import avatar from '@/assets/logo.png'
  25. import { defineComponent } from 'vue'
  26. export default defineComponent({
  27. setup() {
  28. const logout = () => {
  29. console.log('退出登录')
  30. }
  31. return {
  32. logout,
  33. avatar
  34. }
  35. }
  36. })
  37. </script>
  38. <style lang="scss" scoped>
  39. .avatar-container {
  40. margin-right: 30px;
  41. .avatar-wrapper {
  42. margin-top: 5px;
  43. .user-avatar {
  44. width: 40px;
  45. height: 40px;
  46. border-radius: 10px;
  47. cursor: pointer;
  48. }
  49. .el-icon-caret-bottom {
  50. cursor: pointer;
  51. font-size: 12px;
  52. }
  53. }
  54. }
  55. </style>

导入图片时 会报缺少类型声明

ts中图片也需要类型声明

image.png
src下创建 src/images.d.ts

  1. declare module '*.svg'
  2. declare module '*.png'
  3. declare module '*.jpg'
  4. declare module '*.jpeg'
  5. declare module '*.gif'
  6. declare module '*.bmp'
  7. declare module '*.tiff'

3-2 navbar中导入组件

image.png
src/layout/components/Navbar.vue

  1. <template>
  2. <div class="navbar">
  3. <hambuger @toggleClick="toggleSidebar" :is-active="sidebar.opened"/>
  4. <breadcrumb />
  5. <div class="right-menu">
  6. <!-- 全屏 -->
  7. <screenfull id="screefull" class="right-menu-item hover-effect" />
  8. <!-- element组件size切换 -->
  9. <el-tooltip content="Global Size" effect="dark" placement="bottom">
  10. <size-select class="right-menu-item hover-effect" />
  11. </el-tooltip>
  12. <!-- 用户头像 -->
  13. <avatar />
  14. </div>
  15. </div>
  16. </template>
  17. <script lang="ts">
  18. import { defineComponent, computed } from 'vue'
  19. import Breadcrumb from '@/components/Breadcrumb/index.vue'
  20. import Hambuger from '@/components/Hambuger/index.vue'
  21. import { useStore } from '@/store/index'
  22. import Screenfull from '@/components/Screenfull/index.vue'
  23. import SizeSelect from '@/components/SizeSelect/index.vue'
  24. import Avatar from './avatar/index.vue'
  25. export default defineComponent({
  26. name: 'Navbar',
  27. components: {
  28. Breadcrumb,
  29. Hambuger,
  30. Screenfull,
  31. SizeSelect,
  32. Avatar
  33. },
  34. setup() {
  35. // 使用我们自定义的useStore 具备类型提示
  36. // store.state.app.sidebar 对于getters里的属性没有类型提示
  37. const store = useStore()
  38. const toggleSidebar = () => {
  39. store.dispatch('app/toggleSidebar')
  40. }
  41. // 从getters中获取sidebar
  42. const sidebar = computed(() => store.getters.sidebar)
  43. return {
  44. toggleSidebar,
  45. sidebar
  46. }
  47. }
  48. })
  49. </script>
  50. <style lang="scss">
  51. .navbar {
  52. display: flex;
  53. .right-menu {
  54. flex: 1;
  55. display: flex;
  56. align-items: center;
  57. justify-content: flex-end;
  58. padding-right: 15px;
  59. &-item {
  60. padding: 0 8px;
  61. font-size: 18px;
  62. color: #5a5e66;
  63. vertical-align: text-bottom;
  64. &.hover-effect {
  65. cursor: pointer;
  66. transition: background .3s;
  67. &:hover {
  68. background: rgba(0, 0, 0, .025);
  69. }
  70. }
  71. }
  72. }
  73. }
  74. </style>

本节参考源码

https://gitee.com/brolly/vue3-element-admin/commit/c7b2dbbe42e121ab7f29d8958b9994af7fddde75