Javascript 如何对其他元素调用方法? 聚合物(“我的东西”{ athing:function(){return'hello'} });
我想使用上面在下面的元素中定义的元素并访问athing属性Javascript 如何对其他元素调用方法? 聚合物(“我的东西”{ athing:function(){return'hello'} });,javascript,polymer,Javascript,Polymer,我想使用上面在下面的元素中定义的元素并访问athing属性 <link rel="import" href="../bower_components/polymer/polymer.html"> <polymer-element name="my-thing"> <script> Polymer('my-thing', { athing: function () { return 'hello' }
<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>
id为的节点反映在$
哈希中,而不是反映在this
中,因此
this.$.mything.athing();//返回“hello”
[Poster更改了他的示例以包含缺少的$
]
修正语法后,我无法重现您的问题
非常感谢!这实际上是一个输入错误。我很困惑,现在您的示例说:this.$.mything.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>