简体   繁体   English

dart vm有效,但dart2js失败

[英]dart vm works but dart2js fails

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. dart vm一切正常,但是当我尝试部署dart vm时,它会失败(编译是可以的。)当我运行内置的js版本时。 It gave me the error 它给了我错误

Uncaught TypeError: Cannot call method 'shL' of null 未捕获的TypeError:无法调用null的方法“ shL”

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. 我试图跟踪已编译的js代码,这似乎是由于查询了文档中尚未存在的元素(它在html文件中,但是当时只加载了文档的头)。编译版本,很难追踪哪一部分出错了。 Also, there's no error in dart vm and dart2js. 此外,dart vm和dart2js也没有错误。

Does anyone know why this is happening or I did something wrong? 有谁知道为什么会这样,还是我做错了什么?

PS. 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. 我认为,要使dart更加流行,至少dart2js编译器必须与js版本中在vm上代码运行正常的情况一样稳定。 Having developer try to debug on the compiled js code is really annoying. 让开发人员尝试对已编译的js代码进行调试确实很烦人。 Thanks, Yi 谢谢,易

==UPDATE== == ==更新

Here is the html file (before being built) 这是html文件(构建之前)

<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>

Usually this problem is caused by polymer expressions accessing properties which are dropped by pub build s tree-shaking. 通常,此问题是由于聚合物表达式访问属性而引起的,而这些属性由于pub build的树状摇晃而丢失。 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. 如果在运行带有选项debug pub build --mode=debug build时项目能够正常工作,则很可能是原因所在。

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 . 如果删除的字段/方法在您控制的代码中,则可以通过添加@reflectable@observable@published批注之一来解决此问题。 If it s 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. 如果它s third party code (SDK or some 3rd-party library) you can import 'dart:mirrors' and annotate it with @MirrorsUsed(options)` s third party code (SDK or some 3rd-party library) you can import 'dart:mirrors' and annotate it with其中选项列出了成员摇树应保留的位置。

I found that there were two issues in my original code. 我发现原始代码中有两个问题。 1. I shouldn't load the main script before the body is loaded which I think it's also wrong in some of the sample codes in the dart page. 1.在加载正文之前,我不应该加载主脚本,我认为这在dart页面中的某些示例代码中也是错误的。 2. I think initPolymer() doesn't init the elements synchronously. 2.我认为initPolymer()不会同步初始化元素。 Therefore, if I call a method of the element right after the initPolymer, it cannot find the method. 因此,如果我在initPolymer之后立即调用该元素的方法,它将无法找到该方法。

I fixed 1. However, I don't know how to fix 2. I tried initPolymer().run(), it doesn't work either. 我修复了1.但是,我不知道如何解决。2.我尝试了initPolymer()。run(),它也不起作用。 Please let me know if someone knows how to fix it. 如果有人知道如何解决,请告诉我。

Thanks, Yi 谢谢,易

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM