将dart对象传递到js interop中的js函数 [英] Passing Dart objects to js functions in js interop

查看:267
本文介绍了将dart对象传递到js interop中的js函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在玩新的js互操作的dart。到目前为止一切都很直接。但有一件事我不知道是如何处理这样的js东西:

I've been playing around a bit with the new js interop of dart. So far everything was very straight-forward. But one thing I'm not sure about is how to deal with js stuff like this:

MathJax.Hub.Config({
  showProcessingMessages: false,
  showMathMenu: false
  .. many other different options
});

我可以翻译MathJax.Hub.Config部分:

I can translate the MathJax.Hub.Config part:

@JS('MathJax') external MathJaxClass get MathJax;
class MathJaxClass {
  external HubClass get Hub;
}

@JS('MathJax.Hub')
class HubClass {
  external void Config(options);
}

但现在我想有 / code>将 Config 参数作为Dart对象。我不知道如何做到这一点。唯一的办法,我可以得到一些工作是与地图

But now I would like to have the options argument of Config function to be a Dart Object. I'm not sure how to do this. The only way, I can get something working is with a Map:

  MathJax.Hub.Config(new JsObject.jsify({
    'showProcessingMessages': false,
    'showMathMenu': false
  }));

但这绝对不理想。任何想法?

But this is surely not ideal. Any ideas?

推荐答案

语法如下:

@anonymous
@JS()
class Config {
  external bool get showProcessingMessages;
  external bool get showMathMenu;

  external factory Config({bool showProcessingMessages, bool showMathMenu});
}



此处 Config 名称与任何JavaScript名称不匹配,因此您可以为其命名任何您想要的。您可以这样调用:

Here the Config name is not matching any javascript name, so you can name it whatever you want. You can then call it like this:

MathJax.Hub.Config(new Config(
  showProcessingMessages: false,
  showMathMenu: false
));

传递给js函数的对象将是一个常规的javascript对象:

The object passed to the js function, will be a regular javascript object:

这篇关于将dart对象传递到js interop中的js函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆