nativescript-angular icon indicating copy to clipboard operation
nativescript-angular copied to clipboard

Nativescript Code Shared doesn't support a single ts file as component class for both mobile and web platforms

Open MohammedrezaFouladi opened this issue 5 years ago • 1 comments

I used to use a single ts file as component class for both web and mobile platforms but when I created new nativescript code shared project, I realized web build doesn't recognize @nativescript/core imports in ts file and raised error, now I have to create a .tns.ts file for native platforms and ts file for web platform as component class.

MohammedrezaFouladi avatar Nov 06 '20 16:11 MohammedrezaFouladi

Generally, with code sharing application, you will want to move those types of imports into a service. The service ends up with a tns.ts and a .ts version, while your component remains strictly .ts. For example, I have this split-service type thing for local storage, router, drawer, etc. Some of them (such as the drawer service for RadSide Drawer), just have empty methods in the web version of the service. It can be a bit of a pain to set up at first, but you end up with all of your important logic staying DRY.

sublime392 avatar Dec 13 '20 22:12 sublime392