【发布时间】:2017-12-21 12:22:31
【问题描述】:
我在 global.function.js 文件中创建了一个函数作为
function getData(flag) {
if (flag === 1) {
return "one";
}
else {
return "not one";
}
}
然后使用 custom-js-import.html 元素导入:
<script src="global.function.js"></script>
当我尝试在 custom-element.html 中访问上述函数时,我可以在脚本部分访问它,但不能在模板部分访问它。 有什么方法可以访问 HTML 元素中的函数?
<!-- custom-element.html -->
<link rel="import" href="https://polygit.org/components/polymer/polymer-element.html">
<link rel="import" href="custom-js-import.html">
<dom-module id="custom-element">
<template>
<div>
Hello
</div>
<div id="data"></div>
<div>{{getData(1)}}</div><!-- Unable to access this from here -->
<div>{{getLocalData()}}</div>
</template>
<script>
// Define the class for a new element called custom-element
class CustomElement extends Polymer.Element {
static get is() { return "custom-element"; }
constructor() {
super();
}
ready(){
super.ready();
this.$.data.textContent = "I'm a custom-element.";
console.log(getData(1));//can be easily accessed from here
}
getLocalData(){
return "local";
}
}
// Register the new element with the browser
customElements.define(CustomElement.is, CustomElement);
</script>
</dom-module>
【问题讨论】:
标签: javascript polymer-1.0 mixins polymer-2.x polymer-starter-kit