跳到主要内容

useObjectUrl

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

代表对象的响应式 URL。

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

演示

选择文件
对象 URL

用法

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
">Open file</
a
>
</template>

组件用法

此函数还通过 @vueuse/components 包提供了一个无渲染组件版本。了解更多用法

vue
<template>
  <UseObjectUrl v-slot="
url
"
:object
="file">
<
a
:href
="
url
">Open file</
a
>
</UseObjectUrl> </template>

类型声明

ts
/**
 * Reactive URL representing an object.
 *
 * @see https://vueuse.org.cn/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)!: 重构组件并使其保持一致 (#4912)
v12.8.0
7432f - feat(types): 废弃 MaybeRefMaybeRefOrGetter,转而使用 Vue 的原生类型 (#4636)
v12.3.0
59f75 - feat(toValue): 废弃 @vueuse/shared 中的 toValue,转而使用 Vue 的原生函数
0a9ed - feat!: 放弃对 Vue 2 的支持,优化打包并清理 (#4349)
v10.3.0
d3f7e - 修复: 改进参数类型 (#3239)

根据 MIT 许可证发布。