通过飞镖代码实现聚合物元素 [英] Instantiating polymer element via dart code

查看:104
本文介绍了通过飞镖代码实现聚合物元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的Dart代码中实例化一个 Polymer 元素。



/ p>



  @CustomTag('card-component')
类CardComponent extends PolymerElement {
@published
String playerName;

CardComponent.created():super.created();到目前为止,我发现要工作的唯一方法是:


}


$ b <

 元素y = new Element.tag('card-component'); 

但我真正想做的是通过访问器访问我的组件的值(而不是 HtmlElement 的属性)。有没有办法直接在Dart中实例化 CardComponent ,而不使用 Element.tag 方式?我尝试以多种方式使用 CardComponent.created()方法,但它没有工作。



case你想知道为什么我想这样做,我想传递参数到我的构造函数,甚至使用工厂来创建一个理想的世界中的组件。



编辑:阅读这个答案后,我最终能够做到我想要的。我只需要这个到我的组件

  factory CardComponent(String playerName)=> document.createElement(card-component)
..attributes = {
playerName:playerName
};

然后实例化为:

  CardComponent myNewCard = new CardComponent(Pacane); 


解决方案

我不知道通过访问器访问我的组件的值(而不是 HtmlElement 的属性)

 (new Element.tag('card-component')as CardComponent)
..playerName ='blabla';

添加自定义工厂构造函数允许您像使用正常构造函数一样使用元素



class CardComponent extends PolymerElement {
($ card $)


@published
String playerName;

CardComponent.created():super.created();

factory CardComponent CardComponent(){
return new Element.tag('card-component');
}
}





  var cc = new CardComponent().. playerName ='blabla'; 

如果你想从 main()不是来自另一个组件,请确保 main()包含Polymer(初始化代码为什么我在polymer.dart元素上遇到类型错误?如何在聚合物应用程序中实现主要功能) / p>

I'm trying to instantiate a Polymer element in my Dart code.

Here's my element:

@CustomTag('card-component')
class CardComponent extends PolymerElement {
  @published
  String playerName;

  CardComponent.created() : super.created();
}

So far the only way I found to be working was:

Element y = new Element.tag('card-component');

But what I would really like to do, would be to access the values of my component by their accessors (instead of by attributes of HtmlElement). Is there a way to instantiate a CardComponent directly in Dart without using the Element.tag way? I tried using the CardComponent.created() method in multiple ways but it didn't work either.

In case you wonder why I want to do this, well I would like to pass parameters to my constructor, or even use a factory to create the component in an ideal world.

Edit: After reading this answer I ended up being able to do what I wanted. I just had to had this to my component

factory CardComponent(String playerName) => document.createElement("card-component")
  ..attributes = {
    "playerName" : playerName
};

And then instantiate it like this:

CardComponent myNewCard = new CardComponent("Pacane");

解决方案

I don't know what exactly you mean by "to access the values of my component by their accessors (instead of by attributes of HtmlElement)"

(new Element.tag('card-component') as CardComponent)
    ..playerName = 'blabla';

adding a custom factory constructor allows you to use the element as if it had a normal constructor

@CustomTag('card-component')
class CardComponent extends PolymerElement {
  @published
  String playerName;

  CardComponent.created() : super.created();

  factory CardComponent CardComponent() {
    return new Element.tag('card-component');
  }
}

var cc = new CardComponent()..playerName = 'blabla';

If you want to do this from main() not from within another component, ensure that main() contains the Polymer (initialization code why am I getting type error on polymer.dart element?, how to implement a main function in polymer apps)

这篇关于通过飞镖代码实现聚合物元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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