问题
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