如何使用await / async使IE 11兼容以下功能?

问题描述 投票:0回答:1

该代码适用于谷歌浏览器,Microsoft Edge,但它在IE 11中不起作用.IE 11不理解“异步功能”。我需要帮助将异步函数(基本上是下面的代码块)转换为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)
}
---------------------------------------------------------------------------
javascript async-await internet-explorer-11 sweetalert2
1个回答
0
投票

您需要两件事:

  • 因为Swal.fire返回一个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)
  }
});

您可以对其他代码块使用相同的模式。只需用标准功能替换箭头功能即可。

© www.soinside.com 2019 - 2024. All rights reserved.