我是Dart的新手,我想知道我是否可以扩展DivElement类以在一行代码中创建自定义元素.我正在寻找这样的东西;
class RedBox extends DivElement {
RedBox(text) {
this.text = text;
this.style.background = "red";
}
}
RedBox myBox = new RedBox("Hello World");
document.body.append(myBox);
Run Code Online (Sandbox Code Playgroud)
当然,我将拥有更复杂的元素和自定义函数.但总的来说,这可能吗?
当我尝试运行时,我得到:
implicit call to super constructor 'DivElement()'
Run Code Online (Sandbox Code Playgroud)
您可以扩展HTML元素,但有一些要求.你现在遇到的是你需要一个RedBox.created构造函数,它只能重定向到它的超类.created必须是唯一的生成构造函数,尽管您可以添加工厂构造函数.
另一个要求是该元素是使用document.registerElement注册的.
尝试添加此:
class RedBox extends HtmlElement {
RedBox.created() : super.created() {
style.background = "red";
}
factory RedBox(text) => new Element.tag('my-redbox')..text = text;
}
document.registerElement('my-redbox', RedBox);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1007 次 |
| 最近记录: |