Skip to content

Commit

Permalink
perf(projects): move changing document title by locale to global even…
Browse files Browse the repository at this point in the history
…t of composables & add appLoading unmount
  • Loading branch information
honghuangdc committed May 30, 2023
1 parent 5f6caab commit 08e194e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
12 changes: 0 additions & 12 deletions src/App.vue
Expand Up @@ -13,26 +13,14 @@
</template>

<script setup lang="ts">
import { watch } from 'vue';
import { useRoute } from 'vue-router';
import { dateZhCN, zhCN } from 'naive-ui';
import { useI18n } from 'vue-i18n';
import { subscribeStore, useThemeStore } from '@/store';
import { useGlobalEvents } from '@/composables';
const theme = useThemeStore();
const { locale, t } = useI18n();
const route = useRoute();
subscribeStore();
useGlobalEvents();
watch(
() => locale.value,
() => {
document.title = route.meta.i18nTitle ? t(route.meta.i18nTitle) : route.meta.title;
}
);
</script>

<style scoped></style>
20 changes: 20 additions & 0 deletions src/composables/events.ts
@@ -1,14 +1,34 @@
import { effectScope, onScopeDispose, watch } from 'vue';
import { useRoute } from 'vue-router';
import { useEventListener } from '@vueuse/core';
import { useI18n } from 'vue-i18n';
import { useTabStore, useThemeStore } from '@/store';

/** 全局事件 */
export function useGlobalEvents() {
const theme = useThemeStore();
const tab = useTabStore();
const route = useRoute();
const { locale, t } = useI18n();
const scope = effectScope();

/** 页面离开时缓存多页签数据 */
useEventListener(window, 'beforeunload', () => {
theme.cacheThemeSettings();
tab.cacheTabRoutes();
});

scope.run(() => {
// 国际化切换时更新浏览器标签文本
watch(
() => locale.value,
() => {
document.title = route.meta.i18nTitle ? t(route.meta.i18nTitle) : route.meta.title;
}
);
});

onScopeDispose(() => {
scope.stop();
});
}
2 changes: 2 additions & 0 deletions src/main.ts
Expand Up @@ -29,6 +29,8 @@ async function setupApp() {

setupI18n(app);

appLoading.unmount();

// mount app
app.mount('#app');
}
Expand Down

0 comments on commit 08e194e

Please sign in to comment.