我是 Polymer 的新手,并开始使用名为 Polymer 3 - Code like a Google developer 的课程来学习它。
我想调用函数:addContactClick()
从子组件:side-menu
。
为此,我将函数作为参数传递。我在 中定义了它side-menu
并使用它调用它on-click
开始-polymer3.js:
import {
PolymerElement,
html
} from '@polymer/polymer';
import SideMenu from './side-menu.js'
import ContentArea from './content-area.js'
class StartPolymer3 extends PolymerElement {
static get properties() {
return {
addContactClick: {
type: Function,
}
};
}
constructor() {
super();
this.addContactClick = this.addContactClick.bind(this);
}
static get template() {
return html `
<style>
.main-page {
display: grid;
grid-template-columns: 250px 1fr;
}
</style>
<div class="main-page">
<side-menu addContactClick="addContactClick"></side-menu>
<content-area></content-area>
</div>
`;
}
addContactClick() {
console.log('Button Clicked');
}
}
customElements.define('start-polymer3', StartPolymer3);
侧面菜单.js:
import {
PolymerElement,
html
} from '@polymer/polymer';
export default class SideMenu extends PolymerElement {
constructor() {
super();
}
static get properties() {
return {
addContactClick: {
type: Function
},
};
}
static get template() {
return html `
<style>
#side-menu {
background: #323759;
height: 100vh;
padding: 50px 25px;
}
.logo {
text-align: center;
}
.logo img {
width: 50px;
}
.title {
color: #ccced7;
font-weight: 700;
margin: 1rem 0;
}
#side-menu nav a {
color: #ccced7;
text-decoration: none;
text-transform: capitalize;
display: block;
padding: 10px 10px 10px 0;
}
#side-menu nav a span.icon{
padding: 10px 10px 10px 0;
}
</style>
<section id="side-menu">
<div class="logo">
<img src="https://www.dexterousgroup.com.au/wp-content/uploads/2018/03/google-logo-icon-PNG-Transparent-Background.png">
</div>
<div class="menu">
<div class="title">Contacts</div>
<nav>
<a href="#" on-click="addContactClick"><span class="icon"> + </span>Add Contacts</a>
</nav>
</div>
</section>
`;
}
}
customElements.define('side-menu', SideMenu);
当我执行它时,我收到以下错误:listener method addContactClick not defined
handler @ template-stamp.js:92