gpt4 book ai didi

dart - 将监听器添加到DART lang中的自定义元素

转载 作者:行者123 更新时间:2023-12-03 03:45:53 25 4
gpt4 key购买 nike

在JavaScript中,我们可以通过here将侦听器添加到自定义元素中,方法是:

proto.createdCallback = function() {
this.addEventListener('click', function(e) {
alert('Thanks!');
});
};

我尝试将等效的DART代码制作为:

    Element launchElement(){
this.onClick.listen((e)=>print('Thanks!'));
return (shadow);
}

我在这里做错什么了吗?

我的自定义元素的完整代码为:

class MegaButton extends ButtonElement  {
static final tag = 'mega-button';
factory MegaButton()=>new Element.tag('button', tag);

MegaButton.created() : super.created() {
var shadow = this.createShadowRoot();
shadow.text='save';

Element launchElement(){
this.onClick.listen((e)=>print('Thanks!'));
return (shadow);
}

}

注册为:

document.registerElement(MegaButton.tag, MegaButton, extendsTag: 'button');

并通过以下方式调用它:

myDiv.nodes.add(new Element.tag('button', 'mega-button'));

最佳答案

class MegaButton extends ButtonElement {
static final tag = 'mega-button';

factory MegaButton()=> new Element.tag('button', tag);

MegaButton.created() : super.created() {
var shadow = this.createShadowRoot();
shadow.text = 'save';
}

void attached() {
this.onClick.listen((e)=>print('Thanks!'));
}
}

关于dart - 将监听器添加到DART lang中的自定义元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26318400/

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