NativeScript Angular code sharing

Ousmane Barry
3 min readAug 4, 2018

--

You can watch the NativeScript {N} Angular code sharing
webinar
to get the full insights. This post is a quick
sum up of this webinar on how to set up a Web +
Mobile shared project with {N} Angular.

First, you have to setup {N} then follow the steps below to setup a shared project.

Setup Angular CLI

Install the latest version of Angular CLI and {N} Schematics:

Setup Angular CLI

Create new project

Now, we are able to create a {N} project for mobile apps only or create Web + Mobile shared project:

ng new

Shared Project Structure

The ng new will create shared project in a monorepo style: web app + mobile app in the same codebase. These two apps will share more than 80% of code. This is really great considering that the output will be 3 applications: mobile apps (iOS/android) et web app. The specifics things will be the styling and the html that are platforms dependent:

Shared Project Structure

How does Shared Project work ?

The below image speaks by itself :-) :

How does Shared Project work ?

Code splitting in a Shared Project

As said before, the styling and the html should be platform dependent. To achieve such splitting, you must add .tns to target {N}. If you don’t put anything, it targets web app. This suffix can be configured via nsconfig.json file.

Code splitting in a shared project
nsconfig.json file

Launch the Mobile and Web apps

ng serve to run the web app

tns run android --bundle to run the android app

tns run ios --bundle to run the ios app

Happy coding with NativeScript Angular !

Launched apps

--

--

Ousmane Barry
Ousmane Barry

Written by Ousmane Barry

Azure Solutions Architect. Coding most of the time with .NET C#.

Responses (1)