Ionic, ngrx and Angular
building web and mobile apps with one code base
Duncan Hunter
@DuncHunter
�Mobile���
�Web��
University
School Chain
Investment Company
foodzone
�Mobile��
�Web���
�Core��
Agenda
The new Angular and Ionic
Code reuse options
Reusable logic with ngrx
Option 1 - npm packages
Option 2 - nx monorepo
The new Angular and Ionic
Code reuse options
Reusable logic with ngrx
Option 1 - npm packages
Option 2 - nx monorepo
AngularJS is a superheroic JavaScript framework
One Framework
Mobile & Desktop
Angular CLI
Docs
Big Framework
Big Community
Universal
Patterns - ngrx
Stable
Super FAST !
Internet Usage 2009 - 2016
51.3 %
Mobile & Tablet
48.7 %
Desktop
=
Cordova
Ionic
Angular
Open source SDK for building mobile apps
Ionic 1
Ionic 3
Ionic CLI creates your project
$ ionic start myNewProject tabs
cd ./myNewProject
ionic serve
Ionic CLI builds the project for the app store
$ ionic cordova run android --release
One app that looks native on each device
The new Angular and Ionic
Code reuse options
Reusable logic with ngrx
Option 1 - npm packages
Option 2 - nx monorepo
�Mobile��Components
Routing
Services�State�
�Web��Components
Routing
Services
State
�Mobile��Components
Routing�
�Web��Components
Routing�
�Core��
Angular Services
State
Components ?
�Mobile��
�Web���
�Core��
Angular Package Format v5.0
core.metadata.json - Metadata for AOT and angular compiler
core.d.ts - Typescript definitions and code completion
core.js - Flattened ES Module or FESM
core.umd.js - Universal Module Definition
closure compiler annotations
The new Angular and Ionic
Code reuse options
Reusable logic with ngrx
Option 1 - npm packages
Option 2 - nx monorepo
=
RxJS
redux Pattern
ngrx
Angular
ngrx is the redux pattern for Angular
+
+
ngrx promotes moving logic out of components
CONTAINER COMPONENT
SERVICE A
SERVICE A
CONTAINER COMPONENT
SERVICE A
PRESENTATIONAL COMPONENT
SERVICE A
CONTAINER COMPONENT
SERVICE A
PRESENTATIONAL COMPONENT
@Input/@Output
SERVICE A
CONTAINER COMPONENT
SERVICE A
PRESENTATIONAL COMPONENT
@Input/@Output
PRESENTATIONAL COMPONENT
@Input/@Output
SERVICE A
CONTAINER COMPONENT
SERVICE A
PRESENTATIONAL COMPONENT
@Input/@Output
PRESENTATIONAL COMPONENT
@Input/@Output
PRESENTATIONAL COMPONENT
@Input/@Output
SERVICE A
CONTAINER COMPONENT
SERVICE A
PRESENTATIONAL COMPONENT
@Input/@Output
PRESENTATIONAL COMPONENT
@Input/@Output
PRESENTATIONAL COMPONENT
@Input/@Output
CONTAINER
COMPONENT
SERVICE A
SERVICE A
CONTAINER COMPONENT
SERVICE A
SERVICE A
PRESENTATIONAL COMPONENT
@Input/@Output
CONTAINER COMPONENT
PRESENTATIONAL COMPONENT
@Input/@Output
CONTAINER
COMPONENT
SERVICE A
SERVICE A
CONTAINER COMPONENT
SERVICE A
SERVICE A
PRESENTATIONAL COMPONENT
@Input/@Output
CONTAINER COMPONENT
PRESENTATIONAL COMPONENT
@Input/@Output
CONTAINER
COMPONENT
SERVICE A
SERVICE A
CONTAINER COMPONENT
SERVICE A
SERVICE A
PRESENTATIONAL COMPONENT
@Input/@Output
CONTAINER COMPONENT
PRESENTATIONAL COMPONENT
@Input/@Output
CONTAINER
COMPONENT
SERVICE A
SERVICE A
SERVICE B
SERVICE B
CONTAINER COMPONENT
SERVICE A
SERVICE A
PRESENTATIONAL COMPONENT
@Input/@Output
CONTAINER COMPONENT
PRESENTATIONAL COMPONENT
@Input/@Output
CONTAINER
COMPONENT
SERVICE A
SERVICE A
SERVICE B
CONTAINER COMPONENT
SERVICE A
SERVICE B
CONTAINER COMPONENT
SERVICE A
SERVICE A
PRESENTATIONAL COMPONENT
@Input/@Output
CONTAINER COMPONENT
PRESENTATIONAL COMPONENT
@Input/@Output
CONTAINER
COMPONENT
SERVICE A
SERVICE A
SERVICE B
CONTAINER COMPONENT
SERVICE A
SERVICE B
CONTAINER COMPONENT
SERVICE A
SERVICE A
PRESENTATIONAL COMPONENT
@Input/@Output
CONTAINER COMPONENT
PRESENTATIONAL COMPONENT
@Input/@Output
CONTAINER
COMPONENT
SERVICE A
SERVICE A
SERVICE B
CONTAINER COMPONENT
SERVICE A
SERVICE C
SERVICE D
SERVICE B
SERVICE C
SERVICE D
SERVICE B
CONTAINER COMPONENT
SERVICE A
SERVICE A
PRESENTATIONAL COMPONENT
@Input/@Output
CONTAINER COMPONENT
PRESENTATIONAL COMPONENT
@Input/@Output
CONTAINER
COMPONENT
SERVICE A
SERVICE A
SERVICE B
CONTAINER COMPONENT
SERVICE A
SERVICE C
SERVICE D
SERVICE B
SERVICE C
SERVICE D
SERVICE B
CONTAINER COMPONENT
STORE
STORE
PRESENTATIONAL COMPONENT
@Input/@Output
CONTAINER COMPONENT
PRESENTATIONAL COMPONENT
@Input/@Output
CONTAINER
COMPONENT
STORE
STORE
CONTAINER COMPONENT
STORE
Component
Store
Reducers
ACTIONS
SELECTORS
Effects
Services
ACTIONS
NEW STATE
{
type: 'DELETE_COMPANY',
payload: {id:1}
}
{
companies: [
{id:1, name:'SSW'},
{id:2, name:'Microsoft'}
],
contacts: [
{id:1, name:Duncan}
]
};
{
companies: [
{id:2, name:'Microsoft'}
],
contacts: [
{id:1, name:Duncan}
]
};
companies: [
{id:2, name:'Microsoft'}
];
new state
action
{
type: 'DELETE_COMPANY_SUCCESS',
payload: {id:1}
}
action
store state
export class LoginComponent {
constructor(private store: Store<AuthState>) {}
login(authenticate: Authenticate): void {
this.store.dispatch({
type: 'LOGIN',
payload: authenticate
});
}
}
Inject the store
Dispatch an action
export class LayoutComponent implements OnInit {
user$: Store<User>;
constructor(private store: Store<AuthState>) {}
ngOnInit() {
this.user$ = this.store.select(getUser);
}
}
Inject the store
Select state
The new Angular and Ionic
Code reuse options
Reusable logic with ngrx
Option 1 - npm packages
Option 2 - nx monorepo
�Mobile��Components
Routing�
�Web��Components
Routing�
�Core��
Angular Services
State management
@NgModule({
imports: [CommonModule],
declarations: [SampleComponent],
exports: [SampleComponent]
})
export class SampleModule {
static forRoot(): ModuleWithProviders {
return {
ngModule: SampleModule,
providers: [SampleService]
};
}
}
Export components
Register services
import { SampleModule } from '@sample-package/core';
@NgModule({
imports: [
SampleModule.forRoot()
]
})
export class AppModule {}
Register angular module
npm install the package
npm link
cd my-app
npm link my-shared-code
npm install ????
Hosted Package Manager
Paid - like npmjs.org
Private - like verdaccio
npm install from git repo url
"dependencies": {
"@angular/animations": "^4.3.1",
"@angular/router": "^4.0.0",
"@sample/core": "git://github.com/sample/core.git",
"@ngrx/router-store": "^4.0.0",
"@ngrx/store": "^4.0.0",
"rxjs": "^5.1.0",
"zone.js": "^0.8.4"
},
npm packages
The new Angular and Ionic
Code reuse options
Reusable logic with ngrx
Option 1 - npm packages
Option 2 - nx monorepo
nx = angular cli on steroids
�Mobile��
�Web���
�Core��
�Mobile���
�Web���
�Nx libs��
Angular Services
State management
Nx Workspaces - apps & libs
sample-web
layout
sample-mobile
layout
photos
auth
profile
material
data-models
reusable libs
sampe-web libs
sample-mobile libs
import { AuthModule } from '@sample/state/auth';
@NgModule({
imports: [
BrowserModule,
AuthModule.forRoot()
],
declarations: [AppComponent],
bootstrap: [AppComponent]
})
Import nx lib
Register nx lib as a module
duncanhunter.com.au
Monorepo
Where to from here ?
Summary
The new Angular and Ionic
Code reuse options
Reusable logic with ngrx
Option 1 - npm packages
Option 2 - nx monorepo
Thank you
@DuncHunter