简体   繁体   English

如何在Dart代码中创建Dart Polymer自定义元素的实例?

[英]How to create an instance of Dart Polymer custom element in Dart code?

There is a guide on how to create Dart web-ui custom element in Dart code. 有关如何在Dart代码中创建Dart web-ui自定义元素的指南 There is also sample code for this technique 还有这种技术的示例代码

Is there any example on how to create a Dart Polymer custom element from Dart code? 有没有关于如何从Dart代码创建Dart Polymer自定义元素的示例? There is an issue saying that a custom element cannot be created using new Element.html() . 有一个问题是无法使用new Element.html()创建自定义元素。 But in web-ui there was no need to use new Element.html() at all. 但是在web-ui中根本不需要使用new Element.html() Even though web-ui required writing a few lines of code, but at least it worked. 尽管web-ui需要编写几行代码,但至少它有效。 Is there a similar technique for creating Dart Polymer elements from Dart code? 是否有类似的技术从Dart代码创建Dart Polymer元素?

Here is the example code: 这是示例代码:

import 'dart:html';
import 'package:polymer/polymer.dart';

main() {
  querySelector('#add-here').children.add(new Element.tag('my-element'));
}

Notice the use of new Element.tag('my-element') . 注意使用new Element.tag('my-element')

I found a neat little snippet they're using on the source code of chromedeveditor , eg here on Github 我发现他们在chromedeveditor的源代码上使用了一个整洁的小片段, 例如在Github上

They use 他们使用

factory ExampleUi() => new Element.tag('ExampleUi');

so that you could construct the element with: 这样你就可以构造元素:

new ExampleUi();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM