Skip to content

useFavicon

Category
Export Size
362 B
Last Changed
last year

响应式的网站图标

Demo

更改 favicon

用法

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

const icon = useFavicon()

icon.value = 'dark.png' // 更改当前图标

传递源 ref

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

js
import { computed, ref } from 'vue'
import { useFavicon, usePreferredDark } from '@vueuse/core'

const isDark = usePreferredDark()
const favicon = computed(() => isDark.value ? 'dark.png' : 'light.png')

useFavicon(favicon)

当传递了一个源 ref 时,返回的 ref 将与源 ref 相同

ts
const source = ref('icon.png')
const icon = useFavicon(source)

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

类型声明

typescript
export interface UseFaviconOptions extends ConfigurableDocument {
  baseUrl?: string
  rel?: string
}
/**
 * 响应式 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>

Source

SourceDemoDocs

贡献者

Anthony Fu
丶远方
Antério Vieira
babu-ch
azaleta
Mikhailov Nikita
Jelf
Alex Kozack

变更日志

v10.5.0 on 10/7/2023
d1fcc - feat: Add link tag if it doesn't exist (#3444)
v10.0.0-beta.5 on 4/13/2023
cb644 - refactor!: remove isFunction and isString utils
v10.0.0-beta.4 on 4/13/2023
4d757 - feat(types)!: rename MaybeComputedRef to MaybeRefOrGetter
10e98 - feat(toRef)!: rename resolveRef to toRef
v9.2.0 on 9/5/2022
df718 - fix: improve type overload (#2123)
v9.1.0 on 8/4/2022
06e26 - fix: writable return type (#2036)

Released under the MIT License.

Build faster with AI
New Masterclass to help you leverage AI in your Vue workflow.
Get Early Access