2
2
import CategorySecond from ' ./CategorySecond.vue' ;
3
3
import PageNotFound from ' ./PageNotFound.vue' ;
4
4
import { useRoute } from ' vue-router' ;
5
- import { useHead } from ' @unhead/vue' ;
6
- import {
7
- shallowRef ,
8
- defineAsyncComponent ,
9
- watch ,
10
- onMounted ,
11
- onBeforeUnmount
12
- } from ' vue' ;
5
+ import { ref , shallowRef , defineAsyncComponent , watchEffect } from ' vue' ;
13
6
14
7
const props = defineProps ({
15
- // const articles = import.meta.glob('./articles/*.zh.md');
16
8
articles: {
17
9
type: Object ,
18
10
required: true
19
11
},
20
- defaultTitle : String ,
12
+ title : String ,
21
13
pathPrefix: String ,
22
14
pathSuffix: String
23
15
});
24
-
25
- useHead ({ title: props .defaultTitle });
16
+ // const articles = import.meta.glob('./articles/*.zh.md');
26
17
27
18
const route = useRoute ();
28
19
const loadError = shallowRef (false );
29
- const articleComponent = shallowRef ();
30
- const title = shallowRef (props .defaultTitle );
20
+ const articleComponent = ref ();
31
21
32
22
function loadArticle (slug ) {
33
23
const path = ` ${ props .pathPrefix ?? ' ./articles/' }${slug .split (' #' )[0 ]}${props .pathSuffix ?? ' .md' }` ;
@@ -41,27 +31,13 @@ function loadArticle(slug) {
41
31
});
42
32
}
43
33
44
- watch(
34
+ loadArticle(route.params.slug);
35
+ watchEffect(
45
36
() => route.params.slug.split('#')[0],
46
- (newSlug) => loadArticle(newSlug),
47
- { immediate: true }
37
+ () => {
38
+ loadArticle(route.params.slug);
39
+ }
48
40
);
49
-
50
- // Update CategorySecond's title
51
- // I guess there would be a better solution :(
52
- let observer;
53
- onMounted(() => {
54
- observer = new MutationObserver((mutations) => {
55
- title.value = mutations[0].target.text.split('|')[0].trim();
56
- }).observe(document.querySelector('title'), {
57
- subtree: true,
58
- characterData: true,
59
- childList: true
60
- });
61
- });
62
- onBeforeUnmount(() => {
63
- if (observer) console.log(observer.disconnect());
64
- });
65
41
</script>
66
42
67
43
<template>
@@ -70,8 +46,11 @@ onBeforeUnmount(() => {
70
46
<PageNotFound />
71
47
</template>
72
48
<template v-else>
73
- <CategorySecond :title="title" />
74
- <component class="vuepress-markdown-body" :is="articleComponent" />
49
+ <CategorySecond v-if="title" :title="title" />
50
+ <component
51
+ class="vuepress-markdown-body"
52
+ :is="articleComponent"
53
+ ref="mdComponent" />
75
54
</template>
76
55
</div>
77
56
</template>
0 commit comments