gpt4 book ai didi

javascript - 将函数作为参数传递给子组件 polymer 3

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:24:25 25 4
gpt4 key购买 nike

我是 Polymer 的新手,开始使用名为 Polymer 3 - 像 Google 开发人员一样编码的类(class)来学习它。

我想从子组件 side-menu 调用函数:addContactClick()

为此,我将函数作为参数传递。我在 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);

side-menu.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
处理程序@ template-stamp.js:92

最佳答案

以下是您应该做并尝试的更改。

start-polymer3.js

  1. 删除了属性 addContactClick
  2. 更改了 addContactClick() 函数的定义
  3. 添加绑定(bind)而不仅仅是函数名称,同时在 side-menu 元素中传递我们自己的函数。

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 {

};
}

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 add-contact-click="[[addContactClick()]]"></side-menu>
<content-area></content-area>
</div>
`;
}
addContactClick() {
return function() {console.log('Button Clicked')};
}
}

customElements.define('start-polymer3', StartPolymer3);

side-menu.js

  1. addContactClicktypeFunction 更改为String

import {
PolymerElement,
html
} from '@polymer/polymer';

export default class SideMenu extends PolymerElement {


constructor() {
super();
}

static get properties() {
return {
addContactClick: {
type: String
},
};
}

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);

如果仍然有问题,请告诉我。

这是您可以访问的另一个链接,用于解决类似问题:

Passing a function as an attribute value

关于javascript - 将函数作为参数传递给子组件 polymer 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51606711/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com