跳到主要内容

React DOM src object

设置源对象
export function setSrcObject(domElement: Element, tag: string, value: any) {
// We optimistically create the URL regardless of object type. This lets us
// support cross-realms and any type that the browser supports like new types.
// 无论对象类型如何,我们都乐观地创建 URL。这使我们能够支持跨域以及浏览器支持的
// 任何类型,例如新类型。
const url = URL.createObjectURL((value: any));
const loadEvent = tag === 'img' ? 'load' : 'loadstart';
const cleanUp = () => {
// Once the object has started loading, then it's already collected by the
// browser and it won't refer to it by the URL anymore so we can now revoke it.
// 一旦对象开始加载,它就已经被浏览器收集了,浏览器不会再通过 URL 引用它
// 所以我们现在可以撤销它。
URL.revokeObjectURL(url);
domElement.removeEventListener(loadEvent, cleanUp);
domElement.removeEventListener('error', cleanUp);
};
domElement.addEventListener(loadEvent, cleanUp);
domElement.addEventListener('error', cleanUp);
domElement.setAttribute('src', url);
}