<link rel="import" href="../bower_components/polymer/polymer.html">
<polymer-element name="my-thing">
<script>
Polymer('my-thing', {
athing: function () { return 'hello' }
});
</script>
</polymer-element>
我希望在下面的元素中使用上面定义的元素,并有权访问 athing 属性。
<link rel="import" href="../bower_components/polymer/polymer.html">
<link rel="import" href="./mything.html">
<polymer-element name="my-hello"">
<template>
<my-thing id="mything"></my-thing>
</template>
<script>
Polymer('my-hello', {
ready: function () {
this.$.mything.athing() // returns undefined
}
});
</script>
</polymer-element>
最佳答案
具有 id 的节点反射(reflect)在 $
哈希中,而不是 this
中,因此
this.$.mything.athing();//返回“你好”
[海报更改了示例以包含缺少的 .$
]
修复语法后,我无法重现您的问题。
关于javascript - 如何调用其他元素的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22190606/