跳到内容

useFavicon

分类
导出大小
445 B
最后更改
5 天前

响应式网站图标

演示

更改网站图标为

用法

js
import { useFavicon } from '@vueuse/core'

const icon = useFavicon()

icon.value = 'dark.png' // change current icon

传递源 ref

你可以传递一个 ref 给它,源 ref 的更改将自动反映到你的网站图标。

js
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

类型声明

typescript
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
2nofa11
babu-ch
azaleta
Mikhailov Nikita
Jelf
Alex Kozack

更新日志

v12.8.0 于 2025/3/5
7432f - feat(types): 弃用 MaybeRefMaybeRefOrGetter,使用 Vue 原生类型 (#4636)
v12.0.0-beta.1 于 2024/11/21
0a9ed - feat!: 移除 Vue 2 支持,优化 bundles 并清理代码 (#4349)
v10.5.0 于 2023/10/7
d1fcc - feat: 如果链接标签不存在则添加 (#3444)
v10.0.0-beta.5 于 2023/4/13
cb644 - refactor!: 移除 isFunctionisString 工具函数
v10.0.0-beta.4 于 2023/4/13
4d757 - feat(types)!: 将 MaybeComputedRef 重命名为 MaybeRefOrGetter
10e98 - feat(toRef)!: 将 resolveRef 重命名为 toRef

在 MIT 许可证下发布。