Skip to content

Commit d23c719

Browse files
committed
fix(CopyrightInfo): 🎨 use computed instead of watch
1 parent 8b07c2a commit d23c719

File tree

4 files changed

+30
-27
lines changed

4 files changed

+30
-27
lines changed

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@
3838
"dist"
3939
],
4040
"scripts": {
41-
"build": "unbuild",
42-
"dev": "vitepress dev",
43-
"preview": "vitepress preview"
41+
"build": "unbuild"
4442
},
4543
"peerDependencies": {
4644
"@iconify-json/carbon": "^1.2.1",

src/components/AppFooter.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const { theme } = useData<PjtsThemeConfig>()
1010
1111
// 定义一个 ref 来存储动态 key
1212
const componentKey = ref(0)
13-
const frontmatter = ref<Record<string, any>>({})
13+
const frontmatter = ref(route.data?.frontmatter)
1414
const isFrontmatterLoaded = ref(false)
1515
1616
// 更新 key 和 frontmatter 的函数

src/components/CopyrightInfo.vue

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,15 @@ import { data } from '../plugins/CopyrightLoader.data'
66
77
const orgName = useData().theme.value.org
88
9-
// 初始化需要更新的变量
10-
const attrs = ref<Record<string, any> | null>(null)
11-
129
const route = useRoute() // 获取当前路由对象
1310
14-
// 定义一个函数,基于当前路由路径更新数据
15-
function updateData() {
16-
const paths = route.path.replace('.md', '').split('/').filter((item: string) => item !== '')
17-
attrs.value = searchClosestInTrie(data, paths)
18-
}
11+
const attrs = computed(() => {
12+
const paths = route.path
13+
.replace('.md', '')
14+
.split('/')
15+
.filter((item: string) => item !== '')
16+
return searchClosestInTrie(data, paths)
17+
})
1918
2019
function searchClosestInTrie(
2120
that: Trie<Record<string, any>>,
@@ -25,7 +24,11 @@ function searchClosestInTrie(
2524
if (path.length === 0)
2625
return node.value
2726
if (path[0] in node.children) {
28-
let value = searchClosestInTrie(that, path.slice(1), node.children[path[0]])
27+
let value = searchClosestInTrie(
28+
that,
29+
path.slice(1),
30+
node.children[path[0]],
31+
)
2932
if (value === null)
3033
value = node.value
3134
return value
@@ -45,15 +48,6 @@ const authors = computed(() => {
4548
const displayAuthors = computed(() => {
4649
return `${authors.value.join(' , ')}`
4750
})
48-
49-
// 监听页面路由变化,路由变化时更新数据
50-
watch(
51-
() => route.path,
52-
() => {
53-
updateData() // 路由变化时调用更新逻辑
54-
},
55-
{ immediate: true }, // 确保在初次加载时也能更新数据
56-
)
5751
</script>
5852

5953
<template>
@@ -66,24 +60,35 @@ watch(
6660
<span>这篇文章 </span>
6761
<span>{{ `“${attrs!.title}”` }}</span>
6862
<span> 由 </span>
69-
<a v-if="attrs?.copyright?.url" :href="attrs.copyright.url">{{ displayAuthors }}</a>
63+
<a v-if="attrs?.copyright?.url" :href="attrs.copyright.url">{{
64+
displayAuthors
65+
}}</a>
7066
<span v-else>{{ displayAuthors }}</span>
7167
<span> 创作</span>
7268
<span v-if="attrs?.copyright?.org && attrs?.copyright?.license">
7369
,{{ attrs.copyright.org }} 在
74-
<a v-if="attrs?.copyright?.licenseUrl" :href="attrs.copyright.licenseUrl">{{ attrs.copyright.license }}</a>
70+
<a
71+
v-if="attrs?.copyright?.licenseUrl"
72+
:href="attrs.copyright.licenseUrl"
73+
>{{ attrs.copyright.license }}</a>
7574
<span v-else>{{ attrs.copyright.license }}</span>
7675
许可下使用
7776
</span>
7877
<span v-else-if="orgName && attrs?.copyright?.license">
7978
,{{ orgName }} 在
80-
<a v-if="attrs?.copyright?.licenseUrl" :href="attrs.copyright.licenseUrl">{{ attrs.copyright.license }}</a>
79+
<a
80+
v-if="attrs?.copyright?.licenseUrl"
81+
:href="attrs.copyright.licenseUrl"
82+
>{{ attrs.copyright.license }}</a>
8183
<span v-else>{{ attrs.copyright.license }}</span>
8284
许可下使用
8385
</span>
8486
<span v-else-if="attrs?.copyright?.license">
8587
,Project Trans 在
86-
<a v-if="attrs?.copyright?.licenseUrl" :href="attrs.copyright.licenseUrl">{{ attrs.copyright.license }}</a>
88+
<a
89+
v-if="attrs?.copyright?.licenseUrl"
90+
:href="attrs.copyright.licenseUrl"
91+
>{{ attrs.copyright.license }}</a>
8792
<span v-else>{{ attrs.copyright.license }}</span>
8893
许可下使用
8994
</span>

src/theme.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import DefaultTheme from 'vitepress/theme-without-fonts'
1212
import { onMounted } from 'vue'
1313

1414
import Layout from './Layout.vue'
15-
import { addFontSwitchListener } from './plugins/fontSwitcher.js'
15+
import { addFontSwitchListener } from './plugins/fontSwitcher'
1616
import './style.css'
1717

1818
import '@nolebase/vitepress-plugin-enhanced-readabilities/client/style.css'

0 commit comments

Comments
 (0)