-
Notifications
You must be signed in to change notification settings - Fork 312
Expand file tree
/
Copy pathApp.vue
More file actions
66 lines (59 loc) · 1.7 KB
/
App.vue
File metadata and controls
66 lines (59 loc) · 1.7 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<template>
<td-doc-layout direction="column">
<td-header framework="site" :style="headerStyle" />
<router-view />
</td-doc-layout>
</template>
<script setup>
import { computed, onMounted, onBeforeUnmount, watch, getCurrentInstance } from 'vue';
// vue-router@3 无 useRoute 组合式 API,使用实例 proxy.$route,并用 computed 保持响应式
const { proxy } = getCurrentInstance();
const route = computed(() => proxy.$route);
// Computed
const headerStyle = computed(() => {
const { name } = route.value;
const fixedHeaderList = ['home', 'home-en', 'source', 'source-en', 'trade'];
if (fixedHeaderList.includes(name)) {
return {
position: 'fixed',
top: 0,
left: 0,
width: '100%',
zIndex: 1200,
'--bg-color-secondarypage': 'var(--bg-color-navigation)',
'--bg-color-secondarypage-hover': 'var(--bg-color-navigation-hover)',
'--bg-color-secondarypage-select': 'var(--bg-color-navigation-select)',
};
}
return { display: 'none' };
});
// Methods
const handleHashScroll = () => {
const hash = decodeURIComponent(route.value.hash);
requestAnimationFrame(() => {
const id = hash.slice(1);
const anchorEl = document.getElementById(id);
if (!anchorEl) return;
requestAnimationFrame(() => {
window.scrollTo({ top: anchorEl.offsetTop - 88 });
});
});
};
// Lifecycle hooks
onMounted(() => {
window.addEventListener('load', handleHashScroll);
});
onBeforeUnmount(() => {
window.removeEventListener('load', handleHashScroll);
});
// Watch
watch(
route,
(newRoute) => {
if (newRoute && newRoute.meta) {
document.title = newRoute.meta.documentTitle || 'TDesign';
}
},
{ immediate: true },
);
</script>