javascript – 让一个子组件在Polymer 1.0.0中调用他的父API

我有两个使用polymer 1.0.0定义的Web组件,我的问题是访问父公共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公共方法

我已经看到一些文档涉及基于事件的解决方案(听儿童事件),但这并不符合我的需要.除非你告诉我唯一的解决方案..

有任何想法吗?

解决方法

ready: function() {
    this.domHost.getAll()
}

来自文件
http://polymer.github.io/polymer/

“domHost”是

“element whose local dom within which this element is contained”

通过这种方式,您可以访问“父”及其功能.
在我看来,这不是polymer框架中的正确方法.

我在我的项目中使用它只是为了为父级定义回调函数.

(对不起,我的英语不好)

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...