在Dart lang中注册自定义元素 [英] Registering custom element in Dart lang

查看:214
本文介绍了在Dart lang中注册自定义元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下自定义元素,我想将它注册在同一个文件中

I've the below custom element, and I want to register it in the same file

custombtn.dart:

custombtn.dart:

class SubmitFonix extends HtmlElement {

  String name;

  factory SubmitFonix([String name]) => (new Element.tag(tag) as SubmitFonix)
       ..name=name
      ;

  SubmitFonix.created() : super.created() {
  ...
}

目前,我在主函数中注册元素,如下所示:

currently, I'm registering the element in the main function, as this:

void main() {
  document.registerElement(SubmitFonix.tag, SubmitFonix);
 }



我想将注册行添加到custombtn.dart文件,而不是如果我不能!

I wanted to add the registration line to the custombtn.dart file, instead of having it in the main() function, but if I could not!

在polymer.dart,我注意到他们使用下面的语句:

in polymer.dart, I noticed they are using below statement:

@initMethod
upgradePaperFocusable() => registerDartType('paper-focusable', PaperFocusable);

,在其他文件中有:

const initMethod = const InitMethodAnnotation();

class InitMethodAnnotation {
 const InitMethodAnnotation();
}

所以,尝试使用相同的概念,并更新我的自定义元素文件as:

so, tried using the same concept, and updated my custom element file to be as:

    class SubmitFonix extends HtmlElement {

  String name;

  factory SubmitFonix([String name]) => (new Element.tag(tag) as SubmitFonix)
       ..name=name
      ;

  SubmitFonix.created() : super.created() {
  ...
}
@initMethod
upgradeCustomBtn() => document.registerElement(SubmitFonix.tag, SubmitFonix);

在执行时,我在控制台中遇到此错误:

at execution, I got this this error in the console:

Exception: type 'HtmlElement' is not a subtype of type 'SubmitFonix' in type cast.
submit_btn.dart:10
SubmitFonix.SubmitFonix.

submit_btn.dart:10是:factory SubmitFonix([String name])=>标签(标签)为SubmitFonix)

submit_btn.dart:10 is: factory SubmitFonix([String name]) => (new Element.tag(tag) as SubmitFonix)

任何想法?

推荐答案

@initMethod 是一个Polymer注解,你只能使用它,如果你使用Polymer,我假设你不。如果你把 registerElement 从main移开,你就把它放到一个直接或间接从main调用的函数或方法中。

@initMethod is a Polymer annotation and you can only use it if you use Polymer and I assume you do not. If you move the registerElement away from main you have it to put to a function or method which is called directly or indirectly from main.

这篇关于在Dart lang中注册自定义元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆