如何在nativescript中使用本机android小部件类型定义?

时间:2021-04-26 17:03:15

I installed the platform definitions by adding "tns-platform-declarations": "^2.5.2"However, when I use the definitions in tns-platform-declarations/android.d.ts I can't access certain parts of the API. For instance, I can't access android.support.design.widget.Snackbar

我通过添加“tns-platform-declarations”来安装平台定义:“^ 2.5.2”但是,当我使用tns-platform-declarations / android.d.ts中的定义时,我无法访问API的某些部分。例如,我无法访问android.support.design.widget.Snackbar

  private snackbar: android.support.design.widget.Snackbar;

because 'android.support' has no exported member 'design' what am I missing? When I go to code complete, I get only 'android.v4' and v4 does not contain a "design" module.

因为'android.support'没有导出的成员'设计'我缺少什么?当我去代码完成时,我只得到'android.v4'而v4不包含“设计”模块。

I also tried to import directly using the 'app' object as somebody suggested:

我还尝试使用'app'对象直接导入,因为有人建议:

  import * as app from 'application';
  private snackbar = app.android.support.design.widget.Snackbar;
  // error: 'application' has no member 'android'

With the above, I get code completion, but a red squiggly under android and tsc won't compile.

有了上面的代码,我得到了代码完成,但android和tsc下的红色波浪形将无法编译。

The only alternative I have found, is similar to this solution which isn't really a solution, as I can cheat like this, too:

我发现的唯一替代方案类似于这个解决方案,这不是一个真正的解决方案,因为我也可以这样作弊:

declare var android:any;

How do I actually use the TS definitions to compile and get code completion?

我如何实际使用TS定义来编译并获得代码完成?

1 个解决方案

#1


2  

There's an experimental type definitions generating tool for the android sdk, and android support libraries used in the project. Do a build/run with the --androidTypings flag to have d.ts' generated for you.

有一个用于android sdk的实验类型定义生成工具,以及项目中使用的android支持库。使用--androidTypings标志进行构建/运行,以便为您生成d.ts'。

Those should contain the classes that you can't find in the platform-declarations, as they are generated against the versions used in your project.

这些应该包含在平台声明中找不到的类,因为它们是针对项目中使用的版本生成的。

tns build android --androidTypings

tns构建android --androidTypings

#1


2  

There's an experimental type definitions generating tool for the android sdk, and android support libraries used in the project. Do a build/run with the --androidTypings flag to have d.ts' generated for you.

有一个用于android sdk的实验类型定义生成工具,以及项目中使用的android支持库。使用--androidTypings标志进行构建/运行,以便为您生成d.ts'。

Those should contain the classes that you can't find in the platform-declarations, as they are generated against the versions used in your project.

这些应该包含在平台声明中找不到的类,因为它们是针对项目中使用的版本生成的。

tns build android --androidTypings

tns构建android --androidTypings