Pass parameters to Dart Polymer element

前端 未结 3 1651
余生分开走
余生分开走 2021-01-06 00:05

The only solid example I could find for Dart Polymer doesn\'t use any parameters. How can I pass parameters to the template. Is it done through the constructor?

My s

3条回答
  •  余生分开走
    2021-01-06 00:43

    If you are creating a custom element you can use a factory constructor:

    class MyElement extends HtmlElement {
      int _foo;
      factory MyElement(int foo) => new Element.tag('my-tag').._foo = foo;
      MyElement.created() {
         // Careful _foo has not been assigned yet.
      }
    }
    
    main() {
       document.register('my-tag', MyElement);
       var element = new MyElement(42);
    }
    

    I assume this also works for PolymerElements, but I haven't tried this myself.

提交回复
热议问题