Skip to content

useObjectUrl

类别
导出体积
247 B
上次更改
7 months ago

响应式对象的 URL。

通过 URL.createObjectURL() 为提供的 FileBlobMediaSource 创建一个 URL,并在源发生变化或组件卸载时自动释放 URL,通过 URL.revokeObjectURL() 实现。

示例

选择文件:
Object URL:
none

使用方法

vue
<script setup lang="ts">
import { 
useObjectUrl
} from '@vueuse/core'
import {
shallowRef
} from 'vue'
const
file
=
shallowRef
()
const
url
=
useObjectUrl
(
file
)
function
onFileChange
(
event
) {
file
.
value
=
event
.target.files[0]
} </script> <template> <
input
type
="file" @
change
="
onFileChange
">
<
a
:href
="
url
">打开文件</
a
>
</template>

组件使用

vue
<template>
  <UseObjectUrl v-slot="
url
"
:object
="file">
<
a
:href
="
url
">打开文件</
a
>
</UseObjectUrl> </template>

类型声明

ts
/**
 * 响应式 Object URL
 *
 * @see https://vueuse.org/useObjectUrl
 * @param object
 */
export declare function 
useObjectUrl
(
object
:
MaybeRefOrGetter
<Blob | MediaSource | null | undefined>,
):
Readonly
<
Ref
<string | undefined, string | undefined>>

源码

源码演示文档

贡献者

一纸忘忧
Anthony Fu
IlyaL
Anthony Fu
IlyaL
SerKo
Andrew Ferreira
vaakian X
BaboonKing
Michael Hüneburg

更新日志

8c521 - feat(components)!: refactor components and make them consistent (#4912)
7432f - feat(types): deprecate MaybeRef and MaybeRefOrGetter in favor of Vue's native (#4636)
59f75 - feat(toValue): deprecate toValue from @vueuse/shared in favor of Vue's native
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
d3f7e - fix: improve parameter type (#3239)