-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(useTitle): 添加 useTitle 钩子并进行文档更新
- 实现 useTitle 钩子以动态更改浏览器标题。 - 添加 useTitle 钩子的文档和演示。 - 更新 generate-router.js 中的路由配置以包含新的 useTitle 页面。
- Loading branch information
Null
committed
Aug 1, 2024
1 parent
eae7282
commit 199f50e
Showing
14 changed files
with
153 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<template> | ||
<div>{{ title }}</div> | ||
</template> | ||
|
||
<script setup> | ||
import { ref } from 'vue'; | ||
import { useTitle } from 'zhongjiayao_v3_hooks'; | ||
const title = ref('hello zhongjiayao_v3_hooks'); | ||
useTitle(title, { restoreOnUnmount: true }); | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# useTitle | ||
|
||
改变浏览器 title 的 Hook | ||
|
||
<preview path="./demo/index.vue" title="基本使用" description='改变浏览器 title,可动态改变; '></preview> | ||
|
||
## API | ||
|
||
## 参数 | ||
|
||
| 参数 | 说明 | 类型 | 默认值 | | ||
| ----- | -------- | ------------- | ------ | | ||
| title | 页面标题 | `Ref<string>` | - | | ||
|
||
## Options | ||
|
||
| 参数 | 说明 | 类型 | 默认值 | | ||
| ---------------- | ---------------------------------- | --------- | ------- | | ||
| restoreOnUnmount | 组件卸载时,是否恢复上一个页面标题 | `boolean` | `false` | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<template> | ||
<div>{{ title }}</div> | ||
</template> | ||
|
||
<script setup> | ||
import { ref } from 'vue'; | ||
import { useTitle } from 'zhongjiayao_v3_hooks'; | ||
const title = ref('hello zhongjiayao_v3_hooks'); | ||
useTitle(title, { restoreOnUnmount: true }); | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# useTitle | ||
|
||
改变浏览器 title 的 Hook | ||
|
||
<preview path="./demo/index.vue" title="基本使用" description='改变浏览器 title,可动态改变; '></preview> | ||
|
||
## API | ||
|
||
## 参数 | ||
|
||
| 参数 | 说明 | 类型 | 默认值 | | ||
| ----- | -------- | ------------- | ------ | | ||
| title | 页面标题 | `Ref<string>` | - | | ||
|
||
## Options | ||
|
||
| 参数 | 说明 | 类型 | 默认值 | | ||
| ---------------- | ---------------------------------- | --------- | ------- | | ||
| restoreOnUnmount | 组件卸载时,是否恢复上一个页面标题 | `boolean` | `false` | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { isRef, onMounted, onUnmounted, ref, Ref, unref, watch } from 'vue'; | ||
import { isBrowser } from '../utils'; | ||
|
||
export interface UseTitleOptions { | ||
restoreOnUnmount?: boolean; | ||
} | ||
|
||
const DEFAULT_OPTIONS: UseTitleOptions = { | ||
restoreOnUnmount: false, | ||
}; | ||
|
||
function useTitle(title: Ref<string> | string, options: UseTitleOptions = DEFAULT_OPTIONS) { | ||
const titleRef = ref(isBrowser ? document.title : ''); | ||
|
||
if (isRef(title)) { | ||
watch(title, () => { | ||
document.title = title.value; | ||
}); | ||
} else { | ||
document.title = title; | ||
} | ||
|
||
onMounted(() => { | ||
document.title = unref(title); | ||
}); | ||
|
||
onUnmounted(() => { | ||
if (options.restoreOnUnmount) { | ||
document.title = unref(titleRef); | ||
} | ||
}); | ||
} | ||
export default useTitle; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters