我一直在遇到关于在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()
} …Run Code Online (Sandbox Code Playgroud)