为什么这段代码适用于 Chrome 但不适用于 Safari?错误:ReferenceError:找不到变量:Polymer
Mac OSX 10.11.6。
代码:
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<script src="/node_modules/webcomponents.js/webcomponents-lite.min.js"></script>
<link rel="import" href="/node_modules/Polymer/polymer.html">
</head>
<body>
<dom-module id="my-test">
<template>
Hello !
</template>
</dom-module>
<script>
Polymer({
is: 'my-test'
});
</script>
<my-test>
</my-test>
</body>
</html>
最佳答案
这是因为在 Chrome 中 <link rel="import">
被同步处理,而在 Safari 中它首先不被识别(因为不是 native 实现的)。所以Polymer()
解析时未定义。
您应该等待 HTMLImportsLoaded
调用 Polymer() 之前的事件。
<script>
document.addEventListener( "HTMLImportsLoaded", function () {
Polymer({
is: 'my-test'
});
})
</script>
您也可以改用 HTMLImports.whenReady()
如 Polymer documentation 中所述.
<script>
HTMLImports.whenReady( function () {
Polymer({
is: 'my-test'
});
})
</script>
关于javascript - 简单的 polymer 组件在 Safari 10 上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42326513/