简体   繁体   English

如何在 dart2js 创建的 js 代码中从主 function 返回值

[英]How to return values from main function in js code created by dart2js

I have dart code that returns a string when it is ran我有 dart 代码在运行时返回一个字符串

main(){
print("hello");
 return "hello";
}

Now i need to call this from js so i used dart2js to convert it to js.现在我需要从 js 调用它,所以我使用dart2js将其转换为 js。

How i call it from a node.js application:我如何从 node.js 应用程序中调用它:

var sudokuLib = require('./sudoku-lib');
console.log(require('./sudoku-lib'));

I am aware of the large amount of code created by dart2js and it is not a problem since whenever i run the converted js file or the above js code, hello is being printed in the console.我知道 dart2js 创建了大量代码,这不是问题,因为每当我运行转换后的 js 文件或上面的 js 代码时,控制台都会打印 hello。 But the problem is that I want the returned value to be stored in a variable in my js code, since i will be using the value in my node application.但问题是我希望将返回的值存储在我的 js 代码中的变量中,因为我将在我的节点应用程序中使用该值。

Is there any way to do this??有没有办法做到这一点?

I will assume that your dart code is now js code and runing fine:我将假设您的 dart 代码现在是 js 代码并且运行良好:

For exemple like this sudoku-lib.js:例如像这样的 sudoku-lib.js:

function main() {
    return "Hello"
}

// add the code below
module.exports = { main };

in a new js file "file.js" you can simply write:在一个新的 js 文件“file.js”中,你可以简单地写:

const lib = require("./sudoku-lib");
// store it in result var
const result = lib.main();
console.log(`The result is: ${result}`);

Ref node-modules-import-and-use-functions-from-another-file参考node-modules-import-and-use-functions-from-another-file

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

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