i was able to fire off an alert message from dart, but couldn't figure out how to call a function I wrote in another js file from dart. This would have been a great selling point if it was straight forward. I did see this post , which got me started, but i feel there must be a way, so please share the love if you figured it out.
Here's what I've done:
Add this to yaml file:
dependencies: js: hosted: js
Add import statement to top of dart file: import 'package:js/js.dart' as js;
Add this bit of code to show alert message
js.scoped(() { js.context.alert("jump for joy;"); });
Here's the part which I think should work but doesn't: given that I have a javascript function doSomething(), I should be able to call
js.context.doSomething();
First add the js package as dependency in your pubspec.yaml
:
dependencies:
js: any
Then you can use your own js function myFunc()
like that:
import 'package:js/js.dart' as js;
main() {
js.context.myFunc();
}
js.context
is an alias to javascript window
.
See Using JavaScript from Dart: The js Library for more details.
Maybe my answer will be worth it for somebody, so that's why I'm posting a simple JS function call from Dart.
js
package dependency: dependencies: js: any
example.js
: function test() { return 12+20; }
example.js
above inside index.html
with the <script src="...">
tag. @JS() library t; import 'package:js/js.dart'; @JS() external int Test(); class MyOwn { int get value => Test(); }
@override Future<Null> ngOnInit() async => print(MyOwn().value);
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.