我有两个用 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>
如您所见,我想知道如何轻松地从 child 那里访问
getAll
公共(public)方法?我看过一些文档提到基于事件的解决方案(听子事件),但这并不符合我的需要。除非你告诉我唯一可用的解决方案..
有任何想法吗?
最佳答案
ready: function() {
this.domHost.getAll()
}
从文档:
http://polymer.github.io/polymer/
“domHost”是
通过这种方式,您可以访问“父级”及其功能。
在我看来,这不是 Polymer 框架中的正确方法。
我在我的项目中使用它只是为了定义父级的回调函数。
(对不起,我的英语不好)
关于javascript - 在 Polymer 1.0.0 中使子组件调用其父 API,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30583905/