简体   繁体   中英

How to call javascript function from dart using js dependency

I want to call the JavaScript function JSONToCSVConvertor() that is present in function.js file which is same directory in which the below dart file(app_component.dart) is present. I am using the dependency js: ^0.6.0 and I have added that in pubspec.yaml as well. I am not sure as to how to call that JavaScript function from dart file using annotations. Please let me know if the statements in line 1,2 are correct and also how to call the JS function from dart.

Code: 1) app_component.dart

@JS('function') library function.js; import 'package:angular/angular.dart'; import 'dart:convert'; import 'package:js/js.dart'; @Component( selector: 'app-component', templateUrl: 'app_component.html', ) class AppComponent { var name = 'Excel Generation'; @JS("function.JSONToCSVConvertor") external void JSONToCSVConvertor(jsonvalue,Title); showData(){ var jsonData = [{"Vehicle":"BMW","Date":"30, Jul 2013 09:24 AM","Location":"Hauz Kh"}]; var jsonvalue = json.encode(jsonData); String Title = "Excel project"; JSONToCSVConvertor(jsonvalue,Title); } }

I think this introduces the function prefix twice

@JS('function')
library function.js;
...
@JS("function.JSONToCSVConvertor")
   external void JSONToCSVConvertor(jsonvalue,Title);

https://pub.dartlang.org/packages/js shows some examples how to do it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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