How to use the native android widgets type definitions in nativescript?

不想你离开。 提交于 2019-12-31 04:24:08

问题


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

  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.

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

  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.

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?


回答1:


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.

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



来源:https://stackoverflow.com/questions/43079773/how-to-use-the-native-android-widgets-type-definitions-in-nativescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!