注册自定义元素
问题描述:
我有下面的自定义元素,我想在同一个文件进行注册注册自定义元素
custombtn.dart:
class SubmitFonix extends HtmlElement {
String name;
factory SubmitFonix([String name]) => (new Element.tag(tag) as SubmitFonix)
..name=name
;
SubmitFonix.created() : super.created() {
...
}
目前
,我注册在主功能的元件,为这样:
void main() {
document.registerElement(SubmitFonix.tag, SubmitFonix);
}
我想登记的行添加,而不是在主()福具有它的custombtn.dart文件, nction,但如果我不能!
在polymer.dart,我注意到他们使用如下语句:
@initMethod
upgradePaperFocusable() => registerDartType('paper-focusable', PaperFocusable);
和其他文件,他们有:
const initMethod = const InitMethodAnnotation();
class InitMethodAnnotation {
const InitMethodAnnotation();
}
因此,使用相同的概念尝试,并更新我的自定义元件文件如下:
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);
在执行时,我在控制台得到了这个错误:
Exception: type 'HtmlElement' is not a subtype of type 'SubmitFonix' in type cast.
submit_btn.dart:10
SubmitFonix.SubmitFonix.
submit_btn.dart:10是:工厂SubmitFonix([字符串名])=>(新Element.tag(标签)作为SubmitFonix)
任何思想?
答
@initMethod
是聚合物注释,你只能使用它,如果你使用聚合物,我假设你没有。如果你将registerElement
从main移开,你需要将它放到一个直接或间接从main调用的函数或方法中。
感谢@Gunter,您的评论以及此:http://*.com/questions/26821567/custom-annotation-metadata-in-dart-lang/26830363#26830363和此:http://*.com/questions/26826521 /由他们的元数据执行bundle-of-functions-tag-in-dart-lang帮助我解决了这个问题 – 2014-11-10 07:03:47