vue报错TypeError: Failed to execute 'createObjectURL' on 'URL'

TypeError: Failed to execute 'createObjectURL' on 'URL': No function was found that matched the signature provided.
我的写法有问题吗?
image.png

阅读 39.6k
4 个回答

是不是 tsconfig.json 配置的 "lib" 没有加 "dom" 啊。

或者看看你的文件上面有没有

import { URL } from "url"

浏览器已经提供了 window.URL.createObjectURL,但是在 nodejs 中,url 模块也提供了一个 URL 对象。有时候在 vscode 里面写代码时,编辑器会自作聪明的自动补全一个 import { URL } from "url",但是这个 Nodejs 的 URL 是没有 createObjectURL 函数的。

ts 的 dom 库提供了浏览器的 URL 对象,签名是:

declare var URL: {
    prototype: URL;
    new(url: string, base?: string | URL): URL;
    createObjectURL(object: any): string;
    revokeObjectURL(url: string): void;
};
add at 2022.01.05

我今天也遇到这个问题,用的也是 axios ,排查后发现是少传递了 config ,添加下面的配置,就可以了

{
    responseType: 'blob'
}

看图片可能更清晰一点,截图如下:

在小程序里调用时要设置 responseType: 'arraybuffer',截图如下:

小程序里header没有s,而且 Content-Type 是大写,和 axios 中有区别。

window.URL.createObjectURL()

我遇到同样的错误,当我传递给createObjectURL原始数据时:
window.URL.createObjectURL(data)
它必须是Blob或File对象,而不是数据本身。这对我有用:

var binaryData = [];
binaryData.push(data);
window.URL.createObjectURL(new Blob(binaryData, {type: "application/zip"}))

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题