In JavaScript, how I use Dart generated JavaScript file?

前端 未结 1 1862
耶瑟儿~
耶瑟儿~ 2021-01-16 15:46

I know how Dart to use other javascript through dart.js library. How do I use Dart generated JavaScript in other JavaScript? How do I call a function in Dart generated javas

相关标签:
1条回答
  • 2021-01-16 16:08

    Using JS interop, you can call Javascript from Dart, and Dart from Javascript.

    To call Dart from Javascript, you first need to register the functions that you want Javascript to have access to. It's also possible to pass primitives between JS and Dart and vice-versa.

    import 'dart:html';
    import 'dart:js' as js;
    
    void main() {
      js.context["myFunc"] = () {
        print("called from javascript");
      };
    
      js.context["sayGreeting"] = (message) {
        print("greeting: $message");
      };
    }
    

    Then from Javascript, you can call the functions that you registered:

    myFunc(); // prints 'called from javascript'
    sayGreeting("Hello"); // prints 'greeting: Hello'
    
    0 讨论(0)
提交回复
热议问题