Polymer 2.0从父级调用子事件

Nic*_*ign 1 javascript polymer

我一直在遇到关于在Polymer 2.0中从父级调用子事件的错误示例或过时的1.0文档.基本上我想要完成的就是从父级触发子事件.但是,当调用"this"时,我似乎也无法在父元素的树结构中找到引用.

的index.html

<!DOCTYPE html>
<html>
<head>
    <link rel="import" href="/elements/parent-element.html">
</head>
<body>
    <parent-element></parent-element>
    <script src="js/polymer.js"></script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

家长element.html

<link rel="import" href="../bower_components/polymer/polymer.html">
<link rel="import" href="./child-element.html">
<dom-module id="parent-element">

  <template>
    <div>
      parent
      <child-element></child-element>
     </div>
  </template>

  <script>
    class ParentElement extends Polymer.Element {
        constructor(){
            super()

        }

      callChildEvent(){
        // call event
      }

        static get is() { 
            return "parent-element"; 
        }
    }
    customElements.define(ParentElement.is, ParentElement);
  </script>
</dom-module>
Run Code Online (Sandbox Code Playgroud)

儿童element.html

<link rel="import" href="../bower_components/polymer/polymer.html">
<dom-module id="child-element">
  <template>
    <div on-click="customEvent">
        child element
    </div>
  </template>
  <script>
    class ChildElement extends Polymer.Element {
        constructor(){
            super()
        }

        childEvent(){
            console.log('event fired');
        }

        static get is() { 
            return "child-element"; 
        }
    }
    customElements.define(ChildElement.is, ChildElement);
  </script>
</dom-module>
Run Code Online (Sandbox Code Playgroud)

谢谢您的帮助!

Hak*_*anC 5

给一个id

<child-element id="childEl"></child-element>
Run Code Online (Sandbox Code Playgroud)

并且父子的fire子元素功能如下:

this.$.childEl.childEvent();
Run Code Online (Sandbox Code Playgroud)