Will Dart support the use of existing JavaScript libraries?

后端 未结 5 1726
北海茫月
北海茫月 2020-12-07 13:41

I understand Dart compiles to JavaScript, and I read the Dart Language Spec on Libraries, although I didn\'t see an answer there. Also a search on their discussion form for

5条回答
  •  没有蜡笔的小新
    2020-12-07 14:09

    There is now a new simpler way https://pub.dartlang.org/packages/js (currently version 0.6.0-beta.6)

    Make JS classes and functions available to Dart like:

    @JS("JSON.stringify")
    external String stringify(obj);
    
    @JS('google.maps')
    library maps;
    
    // Invokes the JavaScript getter `google.maps.map`.
    external Map get map;
    
    // `new Map` invokes JavaScript `new google.maps.Map(location)`
    @JS()
    class Map {
      external Map(Location location);
      external Location getLocation();
    }
    
    // `new Location(...)` invokes JavaScript `new google.maps.LatLng(...)`
    //
    // We recommend against using custom JavaScript names whenever
    // possible. It is easier for users if the JavaScript names and Dart names
    // are consistent.
    @JS("LatLng")
    class Location {
      external Location(num lat, num lng);
    }
    

    for more see the readme of the package

提交回复
热议问题