访问嵌套Polymer自定义元素中的JavaScript方法

Accessing JavaScript methods inside nested Polymer custom elements

本文关键字:JavaScript 方法 元素 嵌套 Polymer 自定义 访问      更新时间:2023-12-24

我正在尝试访问Polymer自定义元素<general-element>中包含的Javascript方法。此元素包含打开和关闭面板的功能。

这个通用元素用于更具体的元素中,例如:<specific-element></specific-element>。特定元素包含通用元素并为其添加属性,例如:面板标题等。

但是,当在页面中使用<specific-element>时,我无法通过它调用任何打开/关闭方法。我是不是从错误的角度处理了这个问题,有没有方法做到这一点?

编辑:代码示例

删除了一些聚合物代码-示例只是为了展示元素如何协同工作

一般要素:

<dom-module id="general-element">
    <template>
        // Code to define a slide-in panel
    </template>
    <script>
        _openPanel: function() {
            //Apply class to make panel visible
        },
        _closePanel: function() {
            // Apply class to hide panel
        }
    </script>
</dom-module>

特定元素:

<dom-module id="specific-element">
    <template>
        <general-element title="Administration Panel" >
            <h1>Hello</h1>
        </general-element>
    </template>
</dom-module>

用法:

<dom-module id="admin-page">
    <template>
        <button on-click="openPanel"></button>
        <specific-element></specific-element>
    </template>
    <script>
        openPanel: function() {
            // Trying to call general-element _openPanel method
        }
    </script>
</dom-module>

我在这里试图做的是调用openPanel行为,正如在general元素中已经描述的那样。这是因为面板将在多个地方使用,因此继续为特定面板实现打开/关闭方法是没有意义的,因为它们将以相同的方式工作。

这应该能在中工作

openPanel: function() {
  // Trying to call general-element _openPanel method
  $$('specific-element').$$('general-element')._openPanel();   
}