55
Web and Mobile Code Sharing with Angular and NativeScript @sebawita

Web and Mobile Code Sharing with Angular and NativeScript · > tns extension install nativescript-angular-cli > tns generate shared-component name > tns g sc name > tns generate shared-module

  • Upload
    buingoc

  • View
    238

  • Download
    0

Embed Size (px)

Citation preview

Web and Mobile Code Sharing with Angular and NativeScript

@sebawita

Sebastian

Witalec Developer Advocate

@Progress

@sebawita

an open source framework for building truly

native mobile apps with JavaScript. Use web

skills, like TypeScript, Angular, Vue and CSS,

and get native UI and performance on iOS and

Android.

NativeScript is…

“The goal”

🥅

Android iOS

Web

+ =

Architecture

+ =

Angular Architecture

Helping with code sharing

Data Binding

{{value}}

[property] = “value”

(event) = ”handler”

[(ng-model)] =

“property”

Do

m \ N

ati

ve

Co

mp

on

en

t

Template

< >

Compone

nt

{ }

Metadat

a

Property

Binding Event

Binding

Renderer

Compone

nt

{ }

Template

< >

Renederer

Dom

🖥

createElement

setElementPropert

y

attachViewAfter

invokeElementMeth

od

Element

Renderer

Mobile

UI Layer

📱

Compone

nt

{ }

Template

< >

NativeScript

Renederer

createElement

setElementPropert

y

attachViewAfter

invokeElementMeth

od

Element

Component

export class MyComponent {

name = 'Sebastian';

twitter = '@sebawita';

sayHelloTo(name) {

alert(’Hi ' + name);

}

}

Template <div>

name: {{ name }}

twitter: {{ twitter }}

<button (click)="sayHelloTo('web')">Hello Web</button>

</div>

<StackLayout>

<label [text]=“'name' + name”></label>

<label [text]=“'twitter ' + twitter ”></label>

<button (tap)="sayHelloTo('mobile')">Hello

Mobile</button>

</StackLayout >

Dependency Injection

Http

📱 🖥 Http call Http call

🌍

HttpClientModule

import { HttpClientModule }

from '@angular/common/http';

@NgModule({

imports: [

HttpClientModule,

]

NativeScriptHttpClientModule

import { NativeScriptHttpClientModule }

from 'nativescript-angular/http-client';

@NgModule({

imports: [

NativeScriptHttpClientModule,

]

HttpClient

import { HttpClient } from '@angular/common/http';

@Injectable()

export class MyHttpService {

constructor(private http: HttpClient) {

}

getData(url: string) {

return this.http.get(url);

}

}

Code Sharing

How to do it?

Shared Project Structure Monorepo

Services Pipes

SASS Variables

Directives

Web Style {N} Style

Navigation

Modules

Components

Web ngModule

{N} ngModule

Navigation

Components

Web HTML

{N} HTML

TS Class

Build Process

Shared Project Structure Monorepo

Services Pipes

SASS Variables

Directives

Web Style

Navigation

Modules

Components

Web ngModule

Navigation

Components

Web HTML

TS Class

Build Process

{N} Style {N} ngModule {N} HTML

Shared Project Structure Monorepo

Services Pipes

SASS Variables

Directives

{N} Style

Navigation

Modules

Components

{N} ngModule

Navigation

Components

{N} HTML

TS Class

Build Process

Web Style

Web ngModule Web HTML

“HOW DO I EVEN?”

🙄

Cu

sto

m b

uild

How does it work?

Mobile app

Bu

sin

es

s a

s u

su

al

Monorepo

angular-native-seed

{N} Project

Structure Web App

ng serve npm run android npm run ios

How to do code splitting?

Components

Web HTML

TS Class

{N} HTML

NameComponent

name.component.html

name.component.ts

name.component.tns.html

How to do code splitting?

Components

Web HTML

TS Class

{N} HTML

NameComponent

name.component.html

name.component.ts

name.component.tns.html

Web Style

{N} Style

name.component.scss

name.component.tns.scss

“Are there any tools to

help me do the magic?”

🎩

nativescript-angular-cli

> tns extension install nativescript-angular-cli > tns generate shared-component name > tns g sc name > tns generate shared-module name > tns g sm name

“What are the

challenges?”

🙋

angularfire2 vs {N} firebase

Challenge

Library API mismatch

Common Angular

Data Service Interface

Common Service Abstraction

Web

Plugin

{N}

Plugin

Web

Data Service

Components Components

Business Logic

Web

Data Service

Business Logic

Common

Plugin Interface

Common Plugin Abstraction

{N}

Plugin

Web

Plugin Wrapper

{N}

Plugin Wrapper

Components Components

Data Service

Business Logic

Web

Plugin

Demo https://github.com/sebawita/pet-bros

💻 📱

The near future

(almost there)

New Shared project template

🥅

Similar to TeamMaestro Seed, but:

- No separate nativescript build folder

- Shared node_modules

- Supported by {N} team 🎉

New Shared Project

- tns create myApp --ng-shared

- cd myApp

- tns run [ios | android] <= to build {N}

- ng serve <= to build web

New Shared Project

New Webpack build

- With Livesync support

- Configurable .tns file filter

- No need for Gulp

New Webpack build

Integration with

Angular CLI

- Call Angular CLI generator from {N}

projects:

ng [generate | g] name

- Override templates for component and

module

ng g [component | module | pipe | service]

name

Angular CLI Integration

Thank you Schematics

😃

Project Migration

with

Schematics 🥅

Project Migration

Mobile

App

Web

Project

Web

App

• ng new myApp

• tns add mobile

• ng serve

• tns run [ios | andoird]

Mobile add

schematic

Shared

Project

Web add

schematic

Mobile

Project

Project Migration

Mobile

App

Web

App

• tns create myApp

• tns add web

• ng serve

• tns run [ios | andoird]

Shared

Project

Demo https://github.com/nativescript/

nativescript-schematics

🥅

This has a potential for more

😲

HomeComponent

Project Migration: Convert Component

HomeComponent

home.component.html

home.component.ts

home.component.scss

home.component.html

home.component.ts

home.component.tns.htm

l

home.component.scss home.component.tns.scs

s

• tns convert component home

Menu Module

MenuComponent

Menu Module

MenuComponent

Project Migration: Convert Component

menu.component.html

menu.component.ts

menu.component.scss

menu.component.html

menu.component.ts

menu.component.tns.htm

l

menu.component.scss menu.component.tns.scs

s

• tns convert module menu

menu.routes.ts

menu.module.ts

menu.routes.ts menu.common.ts

menu.module.ts menu.module.tns.ts

ng new myApp --mobile / --nativescript

ng add --mobile

ng convert module name

ng serve --mobile

Angular CLI extension

Resources Article:

https://www.nativescript.org/blog/code-sharing-between-web-and-mobile-with-angular-and-nativescript

Talk from {N} Dev Day:

https://www.youtube.com/watch?v=HMPkXk_vXDw

NativeScript Angular CLI:

https://github.com/sebawita/nativescript-angular-cli

Github:

https://github.com/sebawita/pet-bros

https://github.com/telerik/ng2-dashboard

https://github.com/nativescript/nativescript-schematics

THANK YOU

THE END