我有两个使用 Polymer 1.0.0 定义的 Web 组件,我的问题是关于访问父公共(public) API
<dom-module id="x-gallery">
<template is="dom-repeat" items="{{photos}}">
<x-photo photo="{{item}}"></x-photo>
</template>
</dom-module>
<script>
Polymer({
is: 'x-gallery',
...
getAll: function() {
return this.photos;
}
});
</script>
<dom-module id="x-photo">
<template>
<img src="{{photo.src}}">
</template>
</dom-module>
<script>
Polymer({
is: 'x-photo',
properties: {
photo: Object
},
ready: function() {
// HERE ---
// How could I access the x-gallery.getAll public method?
// ...
}
});
</script>
如您所见,我想知道您如何轻松地从子级访问 getAll
公共(public)方法?
我看过一些文档提到基于事件的解决方案(监听子事件),但这并不真正符合我的需要。除非你告诉我唯一可用的解决方案..
有什么想法吗?
最佳答案
ready: function() {
this.domHost.getAll()
}
来自文档: http://polymer.github.io/polymer/
“domHost”是
"element whose local dom within which this element is contained"
通过这种方式,您可以访问“parent”及其功能。 在我看来,这不是 Polymer 框架中的正确方法。
我在我的项目中使用它,只是为了定义父级的回调函数。
(抱歉我的英语不好)
关于javascript - 在 Polymer 1.0.0 中让子组件调用他的父 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30583905/