如何使用 await/async 使以下功能与 IE 11 兼容?
How can I make the following functions IE 11 compatible, using await/async?
该代码在GoogleChrome、Microsoft Edge 中有效,但在IE 11 中不起作用。IE 11 不理解"async function"。我需要帮助将异步函数(基本上是下面的代码块)翻译成 IE 11 可以理解的东西。
我自己解决了 sweetalert2 的一些问题,但这三个对我来说有点困难。
上面写的脚本是我必须要处理的。你认为我需要其他图书馆吗?
为了更清楚地说明,我只是将此代码放在 html 文件中并直接 运行 它,不使用上述以外的任何其他库。
sweetalert2 -- 如何使代码示例兼容 IE11?
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@8"></script>
<!-- Optional: include a polyfill for ES6 Promises for IE11 and Android browser -->
<script src="https://cdn.jsdelivr.net/npm/promise-polyfill@8/dist/polyfill.js"></script>
------------------------------------------------------------------------
const {value: password} = await Swal.fire({
title: 'Enter your password',
input: 'password',
inputPlaceholder: 'Enter your password',
inputAttributes: {
maxlength: 10,
autocapitalize: 'off',
autocorrect: 'off'
}
})
if (password) {
Swal.fire('Entered password: ' + password)
}
-----------------------------------------------------------------------
const {value: file} = await Swal.fire({
title: 'Select image',
input: 'file',
inputAttributes: {
'accept': 'image/*',
'aria-label': 'Upload your profile picture'
}
})
if (file) {
const reader = new FileReader
reader.onload = (e) => {
Swal.fire({
title: 'Your uploaded picture',
imageUrl: e.target.result,
imageAlt: 'The uploaded picture'
})
}
reader.readAsDataURL(file)
}
----------------------------------------------------------------------------
const {value: file} = await Swal.fire({
title: 'Select image',
input: 'file',
inputAttributes: {
'accept': 'image/*',
'aria-label': 'Upload your profile picture'
}
})
if (file) {
const reader = new FileReader
reader.onload = (e) => {
Swal.fire({
title: 'Your uploaded picture',
imageUrl: e.target.result,
imageAlt: 'The uploaded picture'
})
}
reader.readAsDataURL(file)
}
---------------------------------------------------------------------------
您需要做两件事:
- 因为
Swal.fire
returns 一个 Promise,如果你只使用 .then
而不是 await
,你将能够使用任何额外复杂的 Promise
- IE11 不支持箭头函数或解构 - 您可以使用 Babel 快速自动转换这样的代码
例如,对于第一个块:
Swal.fire({
title: 'Enter your password',
input: 'password',
inputPlaceholder: 'Enter your password',
inputAttributes: {
maxlength: 10,
autocapitalize: 'off',
autocorrect: 'off'
}
}).then(function(obj) {
var password = obj.value;
if (password) {
Swal.fire('Entered password: ' + password)
}
});
您可以对其他代码块采用相同的模式。只需将箭头函数替换为标准函数即可。
该代码在GoogleChrome、Microsoft Edge 中有效,但在IE 11 中不起作用。IE 11 不理解"async function"。我需要帮助将异步函数(基本上是下面的代码块)翻译成 IE 11 可以理解的东西。
我自己解决了 sweetalert2 的一些问题,但这三个对我来说有点困难。
上面写的脚本是我必须要处理的。你认为我需要其他图书馆吗?
为了更清楚地说明,我只是将此代码放在 html 文件中并直接 运行 它,不使用上述以外的任何其他库。
sweetalert2 -- 如何使代码示例兼容 IE11?
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@8"></script>
<!-- Optional: include a polyfill for ES6 Promises for IE11 and Android browser -->
<script src="https://cdn.jsdelivr.net/npm/promise-polyfill@8/dist/polyfill.js"></script>
------------------------------------------------------------------------
const {value: password} = await Swal.fire({
title: 'Enter your password',
input: 'password',
inputPlaceholder: 'Enter your password',
inputAttributes: {
maxlength: 10,
autocapitalize: 'off',
autocorrect: 'off'
}
})
if (password) {
Swal.fire('Entered password: ' + password)
}
-----------------------------------------------------------------------
const {value: file} = await Swal.fire({
title: 'Select image',
input: 'file',
inputAttributes: {
'accept': 'image/*',
'aria-label': 'Upload your profile picture'
}
})
if (file) {
const reader = new FileReader
reader.onload = (e) => {
Swal.fire({
title: 'Your uploaded picture',
imageUrl: e.target.result,
imageAlt: 'The uploaded picture'
})
}
reader.readAsDataURL(file)
}
----------------------------------------------------------------------------
const {value: file} = await Swal.fire({
title: 'Select image',
input: 'file',
inputAttributes: {
'accept': 'image/*',
'aria-label': 'Upload your profile picture'
}
})
if (file) {
const reader = new FileReader
reader.onload = (e) => {
Swal.fire({
title: 'Your uploaded picture',
imageUrl: e.target.result,
imageAlt: 'The uploaded picture'
})
}
reader.readAsDataURL(file)
}
---------------------------------------------------------------------------
您需要做两件事:
- 因为
Swal.fire
returns 一个 Promise,如果你只使用.then
而不是await
,你将能够使用任何额外复杂的 Promise - IE11 不支持箭头函数或解构 - 您可以使用 Babel 快速自动转换这样的代码
例如,对于第一个块:
Swal.fire({
title: 'Enter your password',
input: 'password',
inputPlaceholder: 'Enter your password',
inputAttributes: {
maxlength: 10,
autocapitalize: 'off',
autocorrect: 'off'
}
}).then(function(obj) {
var password = obj.value;
if (password) {
Swal.fire('Entered password: ' + password)
}
});
您可以对其他代码块采用相同的模式。只需将箭头函数替换为标准函数即可。