Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
正在从Javascript调用Dart。。。是虫子吗?_Dart_Interop - Fatal编程技术网

正在从Javascript调用Dart。。。是虫子吗?

正在从Javascript调用Dart。。。是虫子吗?,dart,interop,Dart,Interop,在编译以下漂亮的代码时 @JS() library nodejs; import 'package:js/js.dart'; @JS() class Exports { external set functionName(Function function); } @JS() external Exports get exports; void _someDartFunction(dynamic s) { print('Hello from Dirt!'); } void ma

在编译以下漂亮的代码时

@JS()
library nodejs;

import 'package:js/js.dart';

@JS()
class Exports {
  external set functionName(Function function);
}

@JS()
external Exports get exports;

void _someDartFunction(dynamic s) {
  print('Hello from Dirt!');
}

void main(List<String> arguments) {
  exports.functionName = allowInterop(_someDartFunction);
}
结节被称为“nodejs”(在这个上下文中名称不是问题!)。npm使用合适的package.json对模块进行初始化

然后我启动nodejs并需要模块

$ nodejs
> var n = require("nodejs")
ReferenceError: self is not defined
    at Object.main (/home/buergerling/Schreibtisch/dart/nodejs1/node_modules/njs/index.js:2544:28)
    at /home/buergerling/Schreibtisch/dart/nodejs1/node_modules/njs/index.js:3569:9
    at init.currentScript (/home/buergerling/Schreibtisch/dart/nodejs1/node_modules/njs/index.js:3549:7)
    at dartProgram (/home/buergerling/Schreibtisch/dart/nodejs1/node_modules/njs/index.js:3564:5)
得到一个错误。可以通过从编译的index.js文件中取消
self
来解决此问题

这里可以发现问题:

...
  main: function($arguments) {
      var t1 = type$.Function;
      J.set$functionName$x(self.exports, P.allowInterop(D.main___someDartFunction$closure(), t1));
      J.set$functionName$x(self.module.exports, P.allowInterop(D.main___someDartFunction$closure(), t1));
      self.functionName = P.allowInterop(D.main___someDartFunction$closure(), t1);
      P.print("Hello from Dirt!");
    },
    Exports: function Exports() {
    }
...
self.exports
更改为
exports
可以解决问题,模块可以按预期加载和使用


有比手工编辑编译后的文件更优雅的解决方案吗?

没有。不建议使用dart编写js库。
...
  main: function($arguments) {
      var t1 = type$.Function;
      J.set$functionName$x(self.exports, P.allowInterop(D.main___someDartFunction$closure(), t1));
      J.set$functionName$x(self.module.exports, P.allowInterop(D.main___someDartFunction$closure(), t1));
      self.functionName = P.allowInterop(D.main___someDartFunction$closure(), t1);
      P.print("Hello from Dirt!");
    },
    Exports: function Exports() {
    }
...