@observable和@published有什么区别

时间:2014-09-10 15:49:03

标签: dart dart-polymer

在Polymer 0.10.1之前,@published注释在声明它的Polymer元素上创建了一个属性 这与Polymer 0.11.0有所不同。从那时起,需要@PublishedProperty(reflect: true)才能使字段值可用作属性。

似乎由于此更新@published@observable具有相同的效果。我错过了什么吗?

1 个答案:

答案 0 :(得分:2)

@published属性仍然允许您以声明方式在HTML中将该值用作属性本身。那你仍然可以这样做:

<my-element myprop="{{hello}}"></my-element>

更改是通过attributes属性以程序方式访问值,然后您必须包含@PublishedProperty(reflect: true)。 (如果您直接与CustomTag类交互,则情况似乎并非如此。

需要@PublishedProperty(reflect: true)

var x = querySelector('x-foo');
x.bar = "hi";
// ...
print(x.attributes["bar"]);

这还需要@PublishedProperty(reflect: true)

/* CSS */
x-foo[bar="hi"] { background-color: blue; }

这不是:

XFoo x = querySelector('x-foo');
x.bar = "hi";
...
print(x.bar);

这是从New Polymer Release 0.12.0

的讨论组中推断出来的