【问题标题】:How to use a dartscript on your website如何在您的网站上使用 dartscript
【发布时间】:2016-08-27 01:25:38
【问题描述】:

你好,我刚开始学习一些飞镖,我也是这个论坛的新手,

我用 webstorm 创建了超级简单的 webapp,并编写了一个非常简单的 dart 脚本和一个 html 文档。

我的 html 文档的头部:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="scaffolded-by" content="https://github.com/google/stagehand">
    <title>Das bist du!</title>
   <script defer src="http://das-bist-du.com/main.dart" type="application/dart"></script>
   <script defer src="http://das-bist-du.com/packages/browser/dart.js">    </script>
 </head>

<body>
</body>
</html>

飞镖代码:

import 'dart:html';
import 'dart:math';

ButtonElement rndButton;
ImageElement rndImage;  
var images;
var rng;

void main() {
  rndButton = querySelector('#rndButton');
  rndImage = querySelector('#imgScreen');

  images = ['http://das-bist-du.com/onewebmedia/anN0MBo_700b.jpg', 'http://das-bist-du.com/onewebmedia/du1.jpg', 'http://das-bist-du.com/onewebmedia/du2.jpg', 'http://das-bist-du.com/onewebmedia/du3.jpg', 'http://das-bist-du.com/onewebmedia/du3.jpg', 'http://das-bist-du.com/onewebmedia/du4.jpg', 'http://das-bist-du.com/onewebmedia/du5.jpg', 'http://das-bist-du.com/onewebmedia/du6.jpg'];
  rndButton.onClick.listen(pickYou);
}
 void pickYou (Event e) {
  rng = new Random();
  setYou(rng.nextInt((images.length)));
}

void setYou (var rnd) {
  print(images[rnd].toString());
  rndImage.src =  (images[rnd].toString());
}

我知道代码并不漂亮,我只是在测试。 :)

现在我想在网站上同时使用两者,我将 html 页面的代码和 dart.js 上传到网站,并将 src 放在 html 脚本标签中。

在使用 Chromium 在 Webstorm 中进行测试时,它可以工作。 但该脚本不在网站上运行。如何在网站上在线使用 dart?

提前致谢

【问题讨论】:

    标签: html dart dart2js


    【解决方案1】:

    Dart 不直接在任何浏览器中运行,除了 Dartium(基于 Chomium 的 Dart 开发浏览器,包含 Dart VM)。

    当您在项目目录(pubspec.yaml 所在的位置)中运行 pub build 时,您会在 build 子目录中获得一个输出,该输出可以上传到 Web 服务器以供公众使用。

    还有一个转换器可以在pub build 运行时优化脚本标签。 https://pub.dartlang.org/packages/dart_to_js_script_rewriter

    【讨论】:

    • 所以我无法在线使用 Dart vm 吗?我尝试使用 dsrt2js 将我的 dart 代码转换为 js,结果是 8000 行代码,所以我认为我做错了。我稍后会尝试 Pub 构建,谢谢
    • pub build 在后台使用dart2js。也许输出没有被缩小。除了您自己编写的代码之外,Dart 默认还必须包含一些代码,因为它为浏览器抽象(如 jQuery)和其他东西提供了许多附加功能。因此,即使 Dart 示例只是一个“Hello World”,您也不会得到 15 行 JS。
    • 你不能在线使用 Dart VM。 Dartium 不打算在生产中使用,并且没有其他浏览器直接支持 Dart。这是不久前计划的,但由于各种原因,这些计划在一年前被放弃了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-30
    • 1970-01-01
    相关资源
    最近更新 更多