为什么此代码在Chrome上有效,但在Safari上无效?错误:参考错误:找不到变量:聚合物
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中则是先不被识别(因为不是本地实现的)。因此Polymer()
在解析时未定义。
HTMLImportsLoaded
在调用Polymer()之前,您应该等待事件。
<script>
document.addEventListener( "HTMLImportsLoaded", function () {
Polymer({
is: 'my-test'
});
})
</script>
您也可以HTMLImports.whenReady()
按照Polymer文档中的说明使用。
<script>
HTMLImports.whenReady( function () {
Polymer({
is: 'my-test'
});
})
</script>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句