pageTitleGuard.ts 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import type { Router } from 'vue-router';
  2. import { useSetting } from '/@/hooks/core/useSetting';
  3. /**
  4. * 设置页面标题
  5. * @param {*} title :页面标题
  6. */
  7. const setDocumentTitle = (title: string) => {
  8. document.title = title;
  9. const ua = navigator.userAgent;
  10. const regex = /\bMicroMessenger\/([\d.]+)/;
  11. // 兼容
  12. if (regex.test(ua) && /ip(hone|od|ad)/i.test(ua)) {
  13. const i = document.createElement('iframe');
  14. i.src = '/favicon.ico';
  15. i.style.display = 'none';
  16. i.onload = function () {
  17. setTimeout(function () {
  18. i.remove();
  19. }, 9);
  20. };
  21. document.body.appendChild(i);
  22. }
  23. };
  24. export const createPageTitleGuard = (router: Router) => {
  25. router.beforeEach(async (to) => {
  26. // This operation does not require synchronization
  27. setTimeout(() => {
  28. const { globSetting } = useSetting();
  29. if (to.meta.title) {
  30. const { title } = globSetting;
  31. const _title = to.meta.title ? ` ${to.meta.title}-${title} ` : `${title}`;
  32. setDocumentTitle(_title);
  33. }
  34. }, 30);
  35. return true;
  36. });
  37. };