跳到主要内容

useFavicon

分类
导出大小
441 B
上次更改
8 个月前

响应式网站图标 (favicon)

演示

将网站图标更改为

用法

ts
import { 
useFavicon
} from '@vueuse/core'
const
icon
=
useFavicon
()
icon
.
value
= 'dark.png' // change current icon

传入源 ref

你可以传入一个 ref,源 ref 的变化将自动反映到你的网站图标中。

ts
import { 
useFavicon
,
usePreferredDark
} from '@vueuse/core'
import {
computed
} from 'vue'
const
isDark
=
usePreferredDark
()
const
favicon
=
computed
(() =>
isDark
.
value
? 'dark.png' : 'light.png')
useFavicon
(
favicon
)

当传入源 ref 时,返回的 ref 将与源 ref 相同。

ts
const 
source
=
shallowRef
('icon.png')
const
icon
=
useFavicon
(
source
)
console
.
log
(
icon
===
source
) // true

类型声明

ts
export interface UseFaviconOptions extends ConfigurableDocument {
  
baseUrl
?: string
rel
?: string
} /** * Reactive favicon. * * @see https://vueuse.org.cn/useFavicon * @param newIcon * @param options */ export declare function
useFavicon
(
newIcon
:
ReadonlyRefOrGetter
<string | null | undefined>,
options
?: UseFaviconOptions,
):
ComputedRef
<string | null | undefined>
export declare function
useFavicon
(
newIcon
?:
MaybeRef
<string | null | undefined>,
options
?: UseFaviconOptions,
):
Ref
<string | null | undefined>
export type
UseFaviconReturn
=
ReturnType
<typeof
useFavicon
>

来源

源代码演示文档

贡献者

Anthony Fu
IlyaL
Anthony Fu
Antério Vieira
SerKo
2nofa11
巴布-ch
azaleta
Mikhailov Nikita
Jelf
Alex Kozack

更新日志

v12.8.0
7432f - feat(types): 废弃 MaybeRefMaybeRefOrGetter,转而使用 Vue 的原生类型 (#4636)
0a9ed - feat!: 放弃对 Vue 2 的支持,优化打包并清理 (#4349)
v10.5.0
d1fcc - feat: 如果不存在则添加 link 标签 (#3444)

根据 MIT 许可证发布。