我有一个按钮,当被点击/按下时,会触发的click()事件,允许用户上传文件。在每个平台上都能正常工作,但有时候我需要在打开上传窗口之前先发送请求,在大多数平台上都能正常工作,但在Safari / IOS Safari上不行,在Safari中请求已经发送并收到响应,但无法触发的click()事件。
这是我的代码:
async handleClickUpload(origin: string) {
try {
this._setDocumentType({ origin });
const { code: documentCode } = this.currentDocument;
if (this._isDocumentBackVariant(documentCode)) {
await this._variantBackDocumentHandler();
}
this._handleUploadMethod();
} catch (e) {
console.error(e);
}
}
我在这里发出请求并设置文档的变化,(在Safari中运作良好)
async _variantBackDocumentHandler() {
const { variation } = await this.getSpecificDocument("cnh_rg_frente");
console.error("Encontrou variação", variation);
if (!variation) {
this._setDocumentType({ open: true });
throw new Error("Variação não encontrada");
}
this._setDocumentType({ variation });
}
在这种方法中,我选择是否打开用户摄像头或允许用户上传文件,(在Safari上运作良好)。
_handleUploadMethod() {
const { origin = "" } = this.documentType;
if (origin === "camera") {
this.cameraController.open = true;
} else {
this._uploadInputHandler();
}
}
我在这里触发了一个隐藏输入框的点击事件,已经通过控制台输出常量uploadInput并获取该元素(即使在Safari中也是如此)。在每个浏览器中,它都会触发click()并打开上传文件的窗口,但在Safari上却没有任何反应。
_uploadInputHandler() {
const uploadInput: HTMLInputElement | null = document.querySelector("#uploadInput");
uploadInput?.click();
}
这是我的输入
<input
id="uploadInput"
@change="(event) => $emit('receiveFile', event)"
type="file"
style="visibility: hidden"
accept="application/pdf, image/png, image/jpeg"
/>
这是我的 .browserlistrc 文件。
defaults
not IE 11
maintained node versions
last 10 ios_saf versions
last 10 safari versions
这是我的 babel.config.js 文件
const plugins = () => {
const defaultPlugins = [
"@babel/plugin-proposal-optional-chaining",
"@babel/plugin-proposal-throw-expressions",
"@babel/plugin-syntax-throw-expressions",
[
"@babel/plugin-transform-runtime",
{
absoluteRuntime: false,
corejs: false,
helpers: true,
regenerator: true,
useESModules: false,
},
],
];
// if (process.env.NODE_ENV === "production") {
// defaultPlugins.push("transform-remove-console");
// }
return defaultPlugins;
};
module.exports = {
presets: [
[
"@vue/cli-plugin-babel/preset",
{
polyfills: [
"es.array.iterator",
"es.promise",
"es.object.assign",
"es.promise.finally",
"es.symbol",
],
},
],
],
plugins: plugins(),
};
我在我的 main.ts 文件中引入了这两个重要的模块
import "core-js/stable";
import "regenerator-runtime/runtime";
这种方法在所有浏览器上都能正常工作,但是当我需要在IOS Safari上点击之前发送请求时,它就无法工作了,而在其他浏览器上可以。