dart vm工作但dart2js失败 [英] dart vm works but dart2js fails

查看:181
本文介绍了dart vm工作但dart2js失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做聚合物飞镖。一切工作正常dart vm但是当我尝试部署它,它失败(编译是好的。)当我运行内置的js版本。它给了我错误

I am doing polymer dart. Everything works fine with dart vm but when I try to deploy it, it fails (compilation was okay.) When I run the built js version. It gave me the error

未捕获TypeError:无法调用方法'shL'null

Uncaught TypeError: Cannot call method 'shL' of null

编译的js代码,并且似乎是由于查询一个尚未在文档中的元素(它在html文件中,但不知何故只有文档的头部在那时被加载)。
因为它是编译版本,很难跟踪哪个部分出错了。此外,在dart vm和dart2js中没有错误。

I tried to trace the compiled js code, and it seems like due to query an element that is not yet in the document (it is in the html file but somehow only the head of the document is loaded at that time.) Since it's the compiled version, it's really hard to trace which part went wrong. Also, there's no error in dart vm and dart2js.

有人知道为什么会发生这种情况,或者我做错了什么?

Does anyone know why this is happening or I did something wrong?

PS。我想让dart更受欢迎,至少dart2js编译器必须像每当代码运行良好在vm是稳定的在js版本。有开发人员试图调试编译js代码真的很烦人。
谢谢,
Yi

PS. I think to make dart more popular, at least dart2js compiler has to be as stable as whenever the code runs fine on vm is fine in the js version. Having developer try to debug on the compiled js code is really annoying. Thanks, Yi

== UPDATE ==

==UPDATE==

文件(在构建之前)

<html>
  <head>
    <meta charset="utf-8">
    <title>Sample app</title>
    <link rel="stylesheet" href="myHtml.css">

    <link rel="import" href="template/my-element.html">
    <script type="application/dart">export 'main.dart';</script>
    <script src="packages/browser/dart.js"></script>
  </head>
  <body>

        <my-element id="myElement" class="myElement" numOfRow="3"></my-element>

  </body>
</html>


推荐答案

通常这个问题是由聚合物表达式访问属性被 pub build 的树形图标删除。
树摇动保留所有被引用的代码,但聚合表达式不被考虑。
如果你的项目在你运行build时使用选项debug pub build --mode = debug ,那么很可能这是原因。

Usually this problem is caused by polymer expressions accessing properties which are dropped by pub builds tree-shaking. Tree-shaking retains all code that is referenced somewhere but polymer expressions are not considered for this yet. If your project works when you run build with option debug pub build --mode=debug then it's very probably that this is the cause.

如果被删除的字段/方法是在代码中你控制,那么你可以通过添加注释 @reflectable @observable @published
如果它的第三方代码(SDK或一些第三方库),你可以导入'dart:mirrors'并用 @MirrorsUsed(options)其中选项列出了应保留的成员树。

If the dropped field/method is in code you control then you can solve this by just adding one of the annotations @reflectable, @observable, or @published. If its third party code (SDK or some 3rd-party library) you can import 'dart:mirrors' and annotate it with@MirrorsUsed(options)` where the options list the members tree-shaking should retain.

这篇关于dart vm工作但dart2js失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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