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

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

Dec 13, 2018

Download

Documents

buingoc
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 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

Web and Mobile Code Sharing with Angular and NativeScript

@sebawita

Page 2: 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

Sebastian

Witalec Developer Advocate

@Progress

@sebawita

Page 3: 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

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…

Page 4: 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

“The goal”

🥅

Page 5: 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

Android iOS

Web

Page 6: 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

+ =

Page 7: 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

Architecture

+ =

Page 8: 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

Angular Architecture

Helping with code sharing

Page 9: 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

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

Page 10: 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

Renderer

Compone

nt

{ }

Template

< >

Renederer

Dom

🖥

createElement

setElementPropert

y

attachViewAfter

invokeElementMeth

od

Element

Page 11: 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

Renderer

Mobile

UI Layer

📱

Compone

nt

{ }

Template

< >

NativeScript

Renederer

createElement

setElementPropert

y

attachViewAfter

invokeElementMeth

od

Element

Page 12: 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

Component

export class MyComponent {

name = 'Sebastian';

twitter = '@sebawita';

sayHelloTo(name) {

alert(’Hi ' + name);

}

}

Page 13: 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

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 >

Page 14: 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

Dependency Injection

Page 15: 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

Http

📱 🖥 Http call Http call

🌍

Page 16: 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

HttpClientModule

import { HttpClientModule }

from '@angular/common/http';

@NgModule({

imports: [

HttpClientModule,

]

Page 17: 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

NativeScriptHttpClientModule

import { NativeScriptHttpClientModule }

from 'nativescript-angular/http-client';

@NgModule({

imports: [

NativeScriptHttpClientModule,

]

Page 18: 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

HttpClient

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

@Injectable()

export class MyHttpService {

constructor(private http: HttpClient) {

}

getData(url: string) {

return this.http.get(url);

}

}

Page 19: 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

Code Sharing

How to do it?

Page 20: 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

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

Page 21: 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

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

Page 22: 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

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

Page 23: 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

“HOW DO I EVEN?”

🙄

Page 25: 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

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

Page 26: 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

How to do code splitting?

Components

Web HTML

TS Class

{N} HTML

NameComponent

name.component.html

name.component.ts

name.component.tns.html

Page 27: 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

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

Page 28: 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

“Are there any tools to

help me do the magic?”

🎩

Page 29: 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

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

Page 30: 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

“What are the

challenges?”

🙋

Page 31: 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

angularfire2 vs {N} firebase

Page 32: 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

Challenge

Library API mismatch

Page 33: 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

Common Angular

Data Service Interface

Common Service Abstraction

Web

Plugin

{N}

Plugin

Web

Data Service

Components Components

Business Logic

Web

Data Service

Business Logic

Page 34: 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

Common

Plugin Interface

Common Plugin Abstraction

{N}

Plugin

Web

Plugin Wrapper

{N}

Plugin Wrapper

Components Components

Data Service

Business Logic

Web

Plugin

Page 35: 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

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

💻 📱

Page 36: 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

The near future

(almost there)

Page 37: 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

New Shared project template

🥅

Page 38: 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

Similar to TeamMaestro Seed, but:

- No separate nativescript build folder

- Shared node_modules

- Supported by {N} team 🎉

New Shared Project

Page 39: 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

- tns create myApp --ng-shared

- cd myApp

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

- ng serve <= to build web

New Shared Project

Page 40: 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

New Webpack build

Page 41: 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

- With Livesync support

- Configurable .tns file filter

- No need for Gulp

New Webpack build

Page 42: 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

Integration with

Angular CLI

Page 43: 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

- 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

Page 44: 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

Thank you Schematics

😃

Page 45: 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

Project Migration

with

Schematics 🥅

Page 46: 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

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

Page 47: 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

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

Page 48: 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

Demo https://github.com/nativescript/

nativescript-schematics

🥅

Page 49: 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

This has a potential for more

😲

Page 50: 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

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

Page 51: 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

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

Page 52: 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

ng new myApp --mobile / --nativescript

ng add --mobile

ng convert module name

ng serve --mobile

Angular CLI extension

Page 53: 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

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

Page 54: 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

THANK YOU

Page 55: 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

THE END