首页 > 解决方案 > How to use bluebird promises only if browser doesn't support them natively

问题描述

I want to use bluebird promises to support browsers like IE11 when making http requests using axios but I'm not using any sort of build tool to bundle my client side dependencies. I'm just importing the bluebird library from a CDN in my html file. The problem with this is the bluebird promise immediately replaces the native promise on import. Is there any way I can do something similar to this on my client side JS:

if (Promise === undefined)
{
  // Use bluebird promise
}

标签: internet-explorervue.jsinternet-explorer-11axiosbluebird

解决方案


这应该可以解决问题,或者至少非常接近:

if (typeof Promise !== "undefined") {
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = 'path to your bluebird file.js'
    document.getElementsByTagName('head')[0].appendChild(script);
}

推荐阅读