navbar导航添加全屏切换按钮

image.png
切换到全屏
image.png

1-1 实现Screenful组件

主要利用 screenfull npm包实现。screenfull文档说明

安装screenfull

  1. npm install screenfull

src/components/Screenfull/index.vue

  1. <template>
  2. <div>
  3. <svg-icon
  4. :icon-class="isFullscreen ? 'exit-fullscreen' : 'fullscreen'"
  5. @click="handleClick"
  6. />
  7. </div>
  8. </template>
  9. <script lang="ts">
  10. import { defineComponent, getCurrentInstance, onMounted, ref } from 'vue'
  11. import screenfull, { Screenfull } from 'screenfull'
  12. /**
  13. * screenful 文档说明
  14. * https://www.npmjs.com/package/screenfull
  15. */
  16. export default defineComponent({
  17. name: 'ScreenFull',
  18. setup () {
  19. const { proxy } = getCurrentInstance()!
  20. const isFullscreen = ref(false)
  21. const handleClick = () => {
  22. if (screenfull.isEnabled) { // 浏览器是否允许全屏模式
  23. screenfull.toggle()
  24. return
  25. }
  26. return proxy?.$message({
  27. message: 'you browser can not work',
  28. type: 'warning'
  29. })
  30. }
  31. const change = () => {
  32. // 更新当前全屏状态 根据状态切换对应图标
  33. isFullscreen.value = (screenfull as Screenfull).isFullscreen
  34. }
  35. const init = () => {
  36. //
  37. if (screenfull.isEnabled) { // 浏览器是否允许全屏模式
  38. // 监听全屏切换状态
  39. screenfull.on('change', change)
  40. }
  41. }
  42. onMounted(() => {
  43. init()
  44. })
  45. return {
  46. isFullscreen,
  47. handleClick
  48. }
  49. }
  50. })
  51. </script>

navbar导入组件

src/layout/components/Navbar.vue

导入组件并修改right-menu样式

image.png
image.png

  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. </div>
  9. </div>
  10. </template>
  11. <script lang="ts">
  12. import { defineComponent, computed } from 'vue'
  13. import Breadcrumb from '@/components/Breadcrumb/index.vue'
  14. import Hambuger from '@/components/Hambuger/index.vue'
  15. import { useStore } from '@/store/index'
  16. import Screenfull from '@/components/Screenfull/index.vue'
  17. export default defineComponent({
  18. name: 'Navbar',
  19. components: {
  20. Breadcrumb,
  21. Hambuger,
  22. Screenfull
  23. },
  24. setup() {
  25. // 使用我们自定义的useStore 具备类型提示
  26. // store.state.app.sidebar 对于getters里的属性没有类型提示
  27. const store = useStore()
  28. const toggleSidebar = () => {
  29. store.dispatch('app/toggleSidebar')
  30. }
  31. // 从getters中获取sidebar
  32. const sidebar = computed(() => store.getters.sidebar)
  33. return {
  34. toggleSidebar,
  35. sidebar
  36. }
  37. }
  38. })
  39. </script>
  40. <style lang="scss">
  41. .navbar {
  42. display: flex;
  43. .right-menu {
  44. flex: 1;
  45. display: flex;
  46. align-items: center;
  47. justify-content: flex-end;
  48. padding-right: 15px;
  49. }
  50. }
  51. </style>

本节参考源码

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