Skip to content

Commit 98e5ff5

Browse files
committed
添加动态页面标题支持:根据不同页面和语言设置文档标题
1 parent bfe4d00 commit 98e5ff5

File tree

4 files changed

+56
-1
lines changed

4 files changed

+56
-1
lines changed

src/App.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {BrowserRouter as Router, Route, Routes} from 'react-router-dom'
1+
import {HashRouter as Router, Route, Routes} from 'react-router-dom'
22
import './App.css'
33
import NavBar from './components/NavBar'
44
import HomePage from './components/HomePage'
@@ -7,13 +7,15 @@ import ChallengeDetailPage from './components/ChallengeDetailPage'
77
import ChallengeContributePage from './components/ChallengeContributePage'
88
import AboutPage from './components/AboutPage'
99
import GitHubRibbon from './components/GitHubRibbon'
10+
import PageTitle from './components/PageTitle'
1011
import './gh-fork-ribbon.css';
1112
import './styles/github-ribbon-fix.css';
1213

1314
const App = () => {
1415
return (
1516
<Router>
1617
<div className="App">
18+
<PageTitle />
1719
<GitHubRibbon repositoryUrl="https://github.com/JSREP/crawler-leetcode" />
1820
<NavBar/>
1921
<div className="content-wrapper" style={{ padding: '20px 0' }}>

src/components/PageTitle.tsx

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { useEffect } from 'react';
2+
import { useLocation } from 'react-router-dom';
3+
import { useTranslation } from 'react-i18next';
4+
5+
/**
6+
* 页面标题组件
7+
* 根据当前路由自动设置文档标题
8+
*/
9+
const PageTitle = () => {
10+
const location = useLocation();
11+
const { t } = useTranslation();
12+
13+
useEffect(() => {
14+
let title = t('titles.default');
15+
16+
// 根据路径设置不同的标题
17+
if (location.pathname === '/') {
18+
title = t('titles.home');
19+
} else if (location.pathname === '/challenges') {
20+
title = t('titles.challenges');
21+
} else if (location.pathname === '/about') {
22+
title = t('titles.about');
23+
} else if (location.pathname === '/challenge/contribute') {
24+
title = t('titles.contribute');
25+
} else if (location.pathname.startsWith('/challenge/')) {
26+
title = t('titles.challenge');
27+
}
28+
29+
// 设置文档标题
30+
document.title = title;
31+
}, [location.pathname, t]);
32+
33+
// 此组件不渲染任何内容
34+
return null;
35+
};
36+
37+
export default PageTitle;

src/locales/en.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ export default {
77
system: 'System',
88
contribute: 'Contribute'
99
},
10+
titles: {
11+
home: 'Home - Crawler LeetCode',
12+
challenges: 'Challenges - Crawler LeetCode',
13+
about: 'About - Crawler LeetCode',
14+
contribute: 'Contribute - Crawler LeetCode',
15+
challenge: 'Challenge - Crawler LeetCode',
16+
default: 'Crawler LeetCode'
17+
},
1018
home: {
1119
hero: {
1220
title: 'Web Crawler Challenge Collection',

src/locales/zh.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ const zhTranslations = {
77
system: '系统',
88
contribute: '贡献题目'
99
},
10+
titles: {
11+
home: '首页 - 爬虫技术挑战平台',
12+
challenges: '挑战列表 - 爬虫技术挑战平台',
13+
about: '关于 - 爬虫技术挑战平台',
14+
contribute: '贡献题目 - 爬虫技术挑战平台',
15+
challenge: '挑战详情 - 爬虫技术挑战平台',
16+
default: '爬虫技术挑战平台'
17+
},
1018
home: {
1119
hero: {
1220
title: '爬虫技术挑战合集',

0 commit comments

Comments
 (0)