本文介绍了我如何创建一个新的子类扩展一个ButtonElement的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. import dart:html ,我可以创建一个新的子类 MyButton
    ButtonElement 与工厂构造函数,并添加一些新的函数
    getButtonName }
  1. import dart:html , I can create a new subclass MyButton ofButtonElement with factory constructor ,and add some new functionsuch as getButtonName(){} ...
  2. 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    08-03 22:34
    查看更多