2

我创建了一个 Dart 应用程序并从一些天气 API 源中获取天气信息。

只是出于好奇,我想测试一下是否可以将天气信息封装在一个对象中,然后在 Polymer 模板中渲染该对象的属性。

片段是这样的:

HTML 文件:

<polymer-element ...>
  <template>
    Today in {{weather.city}}, the temperature is {{weather.temp}}. 
  </template>
</polymer-element>

和飞镖文件:

@published Weather weather;
...
weather=new Weather.created(a_json_string);

class Weather
{
   String city;
   num temp;

   // The constructor just creates an instance by extracting the city, temp info fromthe JSON string
}

在 Dartium 中,它工作得非常好。

但是,如果我发布构建该应用程序并尝试运行该输出 HTML 文件,它很糟糕:根本没有显示。

4

1 回答 1

1

可能摇树正在丢弃你的领域。当字段仅由标记中的聚合物表达式引用时,摇树不会识别需要的字段,因此会删除它们。

我认为您无论如何都想使用@observable,因为否则值更改不会反映在您的视图中。

class Weather
{
   @observable String city; // or @reflectable String city;
   @observable num temp; // or @reflectable num temp;

   // The constructor just creates an instance by extracting the city, temp info fromthe JSON string
}
于 2013-12-22T09:38:32.103 回答