问题描述
- import
dart:html
,我可以创建一个新的子类MyButton
ButtonElement
与工厂构造函数,并添加一些新的函数
如getButtonName }
import
dart:html
, I can create a new subclassMyButton
ofButtonElement
with factory constructor ,and add some new functionsuch asgetButtonName(){}
...when it's running ,I get a instance
MyButton btn=new MyButton()
但实例btn运行时类型仍然是 ButtonElement
,不能调用 getButtonName()
功能。如果我使用 btn作为MyButton
,那么我会收到此错误
But the instance "btn" runtime type is still
ButtonElement
, and can't call the getButtonName()
function. If I use btn as MyButton
then I get this error
$ b b
这里是代码
Here is the code
class MyButton extends ButtonElement {
factory MyButton(){
return new ButtonElement();
}
String getButtonName(){
return "ButtonName";
}
}
推荐答案
因为此行
return new ButtonElement();
在工厂构造函数中或 MyButton
与 MyButton
有任何关系,它仍然返回 new ButtonElement()
。
is within a factory constructor or MyButton
doesn't mean it has any relation to MyButton
it still returns new ButtonElement()
.
此问题显示了如何创建没有Polymer的自定义元素。
另请参见此讨论
This question extendTag in Dart custom element shows how to create a custom element without Polymer.
See also this discussion https://groups.google.com/a/dartlang.org/forum/#!topic/misc/-z_8sVp_uPY
这篇关于我如何创建一个新的子类扩展一个ButtonElement的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!