在Firefox上使用聚合物的javascript依赖关系的HTML导入困难;Safari
Difficulty using HTML import for javascript dependencies with polymer on Firefox & Safari
我正在构建一个Polymer应用程序,在导入javascript依赖项时遇到困难。我已经复习了这个问题,但解决方案对我不起作用——lodash javascript不能通过HTML导入加载。lodash的使用只是一个例子,我试图以这种方式导入的任何库都存在这个问题。
以下测试代码在chrome中运行良好,但在firefox或safari中则不然(除非我对其进行硫化,否则我不需要这样做)。
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Test Page</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/webcomponentsjs/0.6.0/webcomponents.min.js"></script>
<link rel="import" href="components/lodash/lodash.html">
<link rel="import" href="components/test-component/test-component.html">
</head>
<body fullbleed unresolved>
<script>
window.addEventListener('WebComponentsReady', function(e) {
console.log("Web Components Ready!");
document.body.appendChild(document.createElement("test-component"));
console.log("Index:" + _.now());
});
</script>
</body>
</html>
注1:WebComponentsReady在HTMLImportsLoaded之后启动(请参阅此处)
注2:我也尝试过webcomponentsjs 0.5.5
测试组件.html
<link rel="import" href="../../bower_components/polymer/polymer.html">
<link rel="import" href="../lodash/lodash.html">
<polymer-element name="test-component">
<template>
<style>
:host {
display: block;
font-size: 2em;
color: cornflowerBlue;
}
</style>
<div id='test_elem' on-tap="{{go}}" fit layout horizontal center center-justified>
Click Me!
</div>
</template>
<script>
Polymer({
ready: function() {
console.log("test-component component ready!");
},
go : function(event, detail, sender) {
console.log("Component:" + _.now());
}
});
</script>
</polymer-element>
lodash.html
<script type="application/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.5.0/lodash.min.js"></script>
safari控制台的有序输出:
Web Components Ready!
test-component component ready!
ReferenceError: Can't find variable: _
降低复杂性仍然会带来同样的问题:
index.html(更简单的版本)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Test Page</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/webcomponentsjs/0.6.0/webcomponents.min.js"></script>
<link rel="import" href="components/test-component/test-component.html">
</head>
<body fullbleed unresolved>
<test-component></test-component>
</body>
</html>
页面加载后,我可以随时点击该组件,但错误仍然会发生-lodash.js永远不可用。我确信这里明显缺少一些东西,但我正在努力解决它。
在lodash.html
中,从中删除type="application/javascript"
<script type="application/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.5.0/lodash.min.js"></script>
更新
我在这里提交了一个webcomponents.js错误:https://github.com/webcomponents/webcomponentsjs/issues/273
您可以使用一个现成的lodash组件,如下所示:https://github.com/Waxolunist/lodash-element
相关文章:
- 导入jQuery脚本获胜'我不处理html文件
- 应用程序脚本中导入HTML的数据擦除;谷歌表格
- 在Firefox上使用聚合物的javascript依赖关系的HTML导入困难;Safari
- 在try-catch块内的HTML文件中导入JavaScript文件
- 如何使用JavaScript将数据从excel(xlsx,csv)导入HTML页面
- 导入 html 格式的外部 js 文件
- 在 Grails 中导入 HTML 模板
- 使用 javascript 导入 HTML 代码
- XML 导入 html 无法从 url 导入
- 使用javascript导入HTML中的XML
- 为网页的特定块导入html文件
- 使用javascript导入html文件(以及所有样式表和脚本)
- 使用 HTML 链接版本导入 HTML 文档
- 导入html的外部部分
- 将外部javascript导入html
- 将XML文件导入HTML页面,然后我想在导入的XML标签上使用DOM
- 导入HTML节与支持其他javascript函数
- 将html表导入html页面
- 如何将信息从文本文件导入 html 或 javascript
- 从外部源导入html