pageTitleGuard.ts 1.0 KB
Newer Older
陈文彬 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
import type { Router } from 'vue-router';

import { useSetting } from '/@/hooks/core/useSetting';

/**
 * 设置页面标题
 * @param {*} title  :页面标题
 */
const setDocumentTitle = (title: string) => {
  document.title = title;
  const ua = navigator.userAgent;
  const regex = /\bMicroMessenger\/([\d.]+)/;
  // 兼容
  if (regex.test(ua) && /ip(hone|od|ad)/i.test(ua)) {
    const i = document.createElement('iframe');
    i.src = '/favicon.ico';
    i.style.display = 'none';
    i.onload = function () {
      setTimeout(function () {
        i.remove();
      }, 9);
    };
    document.body.appendChild(i);
  }
};
export const createPageTitleGuard = (router: Router) => {
  router.beforeEach(async (to) => {
    // This operation does not require synchronization
    setTimeout(() => {
      const { globSetting } = useSetting();
      if (to.meta.title) {
        const { title } = globSetting;
        const _title = to.meta.title ? ` ${to.meta.title}-${title} ` : `${title}`;
        setDocumentTitle(_title);
      }
    }, 30);
    return true;
  });
};