【问题标题】:Dart compiled to JS but object notation is not workingDart 编译为 JS 但对象表示法不起作用
【发布时间】:2014-01-10 17:50:24
【问题描述】:

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

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

sn-p 是这样的:

HTML 文件:

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

还有 Dart 文件:

@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 文件,那就糟透了:根本没有显示。

【问题讨论】:

    标签: dart polymer dart2js


    【解决方案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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-31
      • 2016-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多