Lzh on GitHub

响应式网站图标 (favicon)。

Demo

使用

import { useFavicon } from '@vueuse/core'

const icon = useFavicon()

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

传递源 ref

你可以传递一个 ref 给它,源 ref 的变化将自动反映到你的 favicon。

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 完全相同。

const source = shallowRef('icon.png')
const icon = useFavicon(source)

console.log(icon === source) // true

类型声明

export interface UseFaviconOptions extends ConfigurableDocument {
  baseUrl?: string
  rel?: string
}
/**
 * Reactive favicon.
 *
 * @see https://vueuse.org/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>