我正在尝试在 dart 中创建一个自定义元素。它应该只包含 2 个按钮。它从未真正通过施工过程......我做错了什么?
class GraphButton extends Element {
factory GraphButton() => new Element.tag('GraphButton');
ButtonElement colorBtn;
ButtonElement removeBtn;
GraphButton.created() : super.created() {
}
void setup(String buttonText) {
text = buttonText;
//initialize color btn
colorBtn
..id = 'colorBtn' + text
..text = "colorSelector"
..onClick.listen(
(var e) => querySelector('#output').text = id + 'button clicked!');
//initialize remove button
removeBtn
..id = 'removeBtn' + text
..text = 'X'
..onClick.listen(
(var e) => this.remove());
//add to DOM
this.children
..add(colorBtn)
..add(removeBtn);
}
}
最佳答案
您的代码中存在一些问题。
-
以他们的名义setup(...)
您添加的方法没有被调用,因此没有将标题添加到按钮 HtmlElement
DartPad example
也可以看看:
关于web - 如何在 dart 中创建自定义元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37083112/