import { Component } from '@angular/core';
import { HomePage } from './home.page';
import { TabsPage } from './tabs';
@Component({
template: `<ion-nav [root]="rootPage"></ion-nav>`
})
export class AppComponent {
rootPage:any = TabsPage;
}
/*
Copyright 2016 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
*/
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { IonicApp, IonicModule } from 'ionic-angular';
import { AppComponent } from './app.component';
import { HomePage } from './home.page';
import { NgModule } from '@angular/core'
import { StoreModule } from '@ngrx/store';
import { StoreDevtoolsModule } from '@ngrx/store-devtools';
import { bookReducer } from './bookReducer';
import { TabsPage } from './tabs';
import { AboutPage } from './about.page';
@NgModule({
imports: [
BrowserModule,
StoreModule.forRoot({ books: bookReducer }),
IonicModule.forRoot(AppComponent),
StoreDevtoolsModule.instrument()
],
declarations: [ AppComponent, HomePage, TabsPage, AboutPage ],
entryComponents: [ HomePage, TabsPage, AboutPage ],
bootstrap: [ IonicApp ]
})
export class AppModule { }
/*
Copyright 2016 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
*/
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app.module';
const platform = platformBrowserDynamic();
platform.bootstrapModule(AppModule);
/*
Copyright 2016 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
*/
<!DOCTYPE html>
<html>
<head>
<title>Ionic 2 QuickStart</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- 1. Load libraries -->
<!-- Polyfill(s) for older browsers -->
<script src="https://unpkg.com/core-js@2.4.1/client/shim.min.js"></script>
<script src="https://unpkg.com/zone.js@0.8.12?main=browser"></script>
<script src="https://unpkg.com/systemjs@0.19.40/dist/system.src.js"></script>
<!-- 2. Configure SystemJS -->
<script src="systemjs.config.js"></script>
<link href="https://unpkg.com/ionic-angular@3.4.0/css/ionic.css" rel="stylesheet">
</head>
<!-- 3. Display the application -->
<body>
<ion-app></ion-app>
<script>
System.import('app').catch(function(err){ console.error(err); });
</script>
</body>
</html>
<!--
Copyright 2016 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
-->
### Ionic 3.x - ngrx QuickStart
Basic Ionic 2 plunk based on the Angular QuickStart plunk.
/**
* PLUNKER VERSION
* (based on systemjs.config.js in angular.io)
* System configuration for Angular samples
* Adjust as necessary for your application needs.
*/
(function (global) {
System.config({
// DEMO ONLY! REAL CODE SHOULD NOT TRANSPILE IN THE BROWSER
transpiler: 'ts',
typescriptOptions: {
tsconfig: true
},
meta: {
'typescript': {
"exports": "ts"
}
},
paths: {
// paths serve as alias
'npm:': 'https://unpkg.com/'
},
// map tells the System loader where to look for things
map: {
// our app is within the app folder
app: 'app',
// angular bundles
'@angular/core': 'npm:@angular/core@4.2.2/bundles/core.umd.js',
'@angular/common': 'npm:@angular/common@4.2.2/bundles/common.umd.js',
'@angular/compiler': 'npm:@angular/compiler@4.2.2/bundles/compiler.umd.js',
'@angular/platform-browser': 'npm:@angular/platform-browser@4.2.2/bundles/platform-browser.umd.js',
'@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic@4.2.2/bundles/platform-browser-dynamic.umd.js',
'@angular/http': 'npm:@angular/http@4.2.2/bundles/http.umd.js',
'@angular/forms': 'npm:@angular/forms@4.2.2/bundles/forms.umd.js',
'ionic-angular': 'npm:ionic-angular@3.4.0',
'rxjs': 'npm:rxjs@5.4.1',
'ts': 'npm:plugin-typescript@5.2.7/lib/plugin.js',
'typescript': 'npm:typescript@2.2.1/lib/typescript.js',
'@ngrx/store': 'https://npmcdn.com/@ngrx/store@latest',
'@ngrx/core': 'https://npmcdn.com/@ngrx/core@latest',
'@ngrx/store-devtools': 'https://npmcdn.com/@ngrx/store-devtools@latest',
},
// packages tells the System loader how to load when no filename and/or no extension
packages: {
app: {
main: './main.ts',
defaultExtension: 'ts'
},
rxjs: {
defaultExtension: 'js'
},
'ionic-angular': {
main: './umd/index.js',
defaultExtension: 'js'
}
}
});
})(this);
/*
Copyright 2016 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
*/
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": true,
"suppressImplicitAnyIndexErrors": true
}
}
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Store } from '@ngrx/store';
import { Observable } from 'rxjs/Observable';
import { AppState } from './bookReducer';
import * as BookActions from './bookActions';
@Component({
selector: 'page-home',
templateUrl: 'app/home.page.html'
})
export class HomePage {
appName = 'Ionic App';
books: Observable<any>;
constructor(
public navController: NavController,
private store: Store<AppState>
) {
this.books = store.select<any>('books');
}
addBook(_book){
let id = new Date().getTime()
this.store.dispatch( new BookActions.AddBook({title : _book.value, id }))
}
removeBook(_book) {
this.store.dispatch( new BookActions.DeleteBook({ id:_book.id }));
}
reset(_book){
this.store.dispatch(new BookActions.Delet);
}
}
<ion-header>
<ion-navbar>
<ion-title>Ionic App - Test ngrx4 - Tabs</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<button ion-button (click)="addBook(title)">Add Book</button>
<button ion-button (click)="reset()">Reset Book List</button>
<div>
<ion-item>
<ion-label color="primary">Title</ion-label>
<ion-input placeholder="Book Title" #title></ion-input>
</ion-item>
<ion-item>Click Book Button To Remove Book</ion-item>
<button ion-button *ngFor="let book of books | async" (click)="removeBook(book)">
{{book.title}}
</button>
</div>
</ion-content>
// bookReducer.ts
import { ActionReducer, Action } from '@ngrx/store';
import * as BookActions from './bookActions';
export type Action = BookActions.All;
export interface Book {
title: string,
author : string,
}
export interface AppState {
books: [Book];
}
export function bookReducer(state:[Book] = [], action: Action) {
console.log(action)
switch (action.type) {
case BookActions.ADD_BOOK:
return [...state, ...action.payload];
case BookActions.DELETE_BOOK:
return state.filter((b)=> b.id !== action.payload.id)
case BookActions.RESET:
return [];
default:
return state;
}
}
import { Component } from '@angular/core';
import { AboutPage } from './about.page';
import { HomePage } from './home.page';
@Component({
template: `<ion-tabs>
<ion-tab [root]="tab1Root" tabTitle="Home" tabIcon="home"></ion-tab>
<ion-tab [root]="tab2Root" tabTitle="About" tabIcon="information-circle"></ion-tab>
</ion-tabs>
`
})
export class TabsPage {
tab1Root = HomePage;
tab2Root = AboutPage;
constructor() {
}
}
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Store } from '@ngrx/store';
import { Observable } from 'rxjs/Observable';
import { AppState } from './bookReducer';
@Component({
selector: 'page-about',
templateUrl: 'app/about.page.html'
})
export class AboutPage {
appName = 'Ionic App';
books: Observable<any>;
constructor(
public navController: NavController,
private store: Store<AppState>
) {
this.books = store.select<any>('books');
}
}
<ion-header>
<ion-navbar>
<ion-title>ABOUT PAGE</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<ion-item *ngFor="let book of books | async" (click)="removeBook(book)">
{{book.title}}
</ion-item >
</ion-content>
// bookActions.ts
import { Action } from '@ngrx/store';
import { Book } from './bookReducer'
export const ADD_BOOK = 'ADD_BOOK';
export const DELETE_BOOK = 'DELETE_BOOK';
export const RESET = 'RESET';
export class AddBook implements Action {
readonly type = ADD_BOOK;
constructor(public payload: Book) {
}
}
export class DeleteBook implements Action {
readonly type = DELETE_BOOK;
constructor(public payload: any) {
}
}
export class Reset implements Action {
readonly type = RESET;
constructor(public payload: Book) {}
}
export type All
= AddBook
| DeleteBook
| Reset;