【发布时间】:2018-08-29 01:26:57
【问题描述】:
我正在尝试让 Web 组件和 HTML 导入在 Firefox 和 IE 中工作。
我按照the Web Components GitHub repo 的说明进行操作,通过 npm 安装文件,并将其包含在我的文档开头。
我有一个在文档正文中调用的自定义脚本。
在 firefox 中,polyfill 是动态(同步)加载的,但会将 body 中的 script 标签从:
<script type="module" src="./src/scripts/init.js"></script>
到
<script src="/components/requirejs/require.js"></script>
<script>require(["./src/scripts/init.js"]);</script>
我收到以下错误:
ReferenceError: require is not defined.
我也尝试关注this StackOverflow answer 并单独下载了 polyfill:
(旁注:是否建议从 repo 文件中复制/粘贴原始代码?我不知道这样做的另一种方法。我也发现实际上 locating 正确文件,因为有时该文件位于根文件夹中,有时位于“src”中。我错过了什么吗?)
我对@987654334@ 中的文件进行排序,如下所示:
<!-- <script src="src/scripts/helper/web-components-polyfill.js"></script> -->
<script type="text/javascript" src="src/scripts/helper/html-imports-polyfill.js"></script>
<script type="text/javascript" src="src/scripts/helper/custom-element-polyfill.js"></script>
注意:当我尝试遵循参考问题的建议时,我注释掉了“通用”Web 组件 polyfill。
在 Firefox 和 IE 中,我得到同样的错误:require is not defined。我在 Firefox 中得到了这个额外的好处:
我还尝试使用特征检测来按照 WebComponents.org 加载 polyfill:
<script type="text/javascript">
(function() {
if ('registerElement' in document
&& 'import' in document.createElement('link')
&& 'content' in document.createElement('template')) {
// platform is good!
} else {
// polyfill the platform!
console.log('loading the polyfills');
var e = document.createElement('script');
e.type = "text/javascript";
e.src = './src/scripts/helper/html-imports-polyfill.js';
document.head.appendChild(e);
var f = document.createElement('script');
f.src = './src/scripts/helper/custom-elements-polyfill.js';
document.head.appendChild(f);
}
})();
</script>
启动应用程序的脚本,init.js,我在 polyfill 被“加载”后调用,它被设置为导入 HTML 片段并将它们附加到文档中。这是我用来执行此操作的函数:
/**
* Dynamically imports HTML into the Main file
*
* @param {String} path The path to the document to import
* @return {[type]} [description]
*/
function _importHTML(path) {
console.log('importHTML called', path);
let link = document.createElement('link');
link.rel = 'import';
link.href = path;
link.onload = (e) => {
// console.log('Successfully loaded', e.target.href);
}
link.onerror = (e) => {
console.log('Error loading', e.target.href);
}
document.head.appendChild(link);
}
不用说,但在 Chrome 中一切正常。
请帮忙! :D
【问题讨论】:
-
你更新了 IE 和 firefox 的最新补丁了吗?
-
我不确定我是否理解。目标是定义可以在大多数浏览器中运行的代码,这样我就没有机会更新其他人的浏览器了。我是否正确理解了您的评论?
-
是的,你是对的。我想问的是,如果机器上的浏览器补丁从未更新过,那么它会产生问题。
-
我在角度框架中遇到过类似的问题。有一个名为“polyfills.ts”的文件。我更新了文件,如下
/** IE9, IE10 and IE11 requires all of the following polyfills.**/ import 'core-js/es6/symbol'; import 'core-js/es6/object'; import 'core-js/es6/function'; import 'core-js/es6/parse-int'; import 'core-js/es6/parse-float'; import 'core-js/es6/number'; import 'core-js/es6/math'; import 'core-js/es6/string'; import 'core-js/es6/date'; import 'core-js/es6/array'; import 'core-js/es6/regexp'; import 'core-js/es6/map'; import 'core-js/es6/set'; -
我不确定它们是否更新过。我正在使用 IE11 和 Firefox 60+。我不确定如何更新补丁,但我会调查它。感谢您的评论。
标签: internet-explorer firefox web-component polyfills html-imports