U ovom ću blog postu pokazati kako spriječiti da se AppComponent učitava nakon HomePage.
Kada se kreira nova Ionic aplikacija koristeći tabs predložak kao rootPage odabrana je TabsPage komponenta koja unutar sebe poziva HomePage.
U tom slučaju ako se nekakva logika stavi unutar app.component.ts i onda ovisno o nekom uvjetu učita HomePage ili neka druga stranica
Kreiranje aplikacije
Novu aplikaciju kreiram naredbom
|
1 2 |
$ ionic start IonicTabaApp tabs $ cd IonicTabaApp |
I nakon što pokrenem aplikaciju
|
1 |
$ ionic serve |
mogu vidjeti da se HomePage učitala prije AppComponent.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import { Component } from '@angular/core'; import { Platform } from 'ionic-angular'; import { StatusBar } from '@ionic-native/status-bar'; import { SplashScreen } from '@ionic-native/splash-screen'; import { TabsPage } from '../pages/tabs/tabs'; @Component({ templateUrl: 'app.html' }) export class MyApp { rootPage:any = TabsPage; constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen) { platform.ready().then(() => { console.log('AppComponent'); // Okay, so the platform is ready and our plugins are available. // Here you can do any higher level native things you might need. statusBar.styleDefault(); splashScreen.hide(); }); } } |
Uzrok tome nalazi se unutar app.component.ts datoteke gdje se definira rootPage. U ovom slučaju to je TabsPage.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import { Component } from '@angular/core'; import { AboutPage } from '../about/about'; import { ContactPage } from '../contact/contact'; import { HomePage } from '../home/home'; @Component({ templateUrl: 'tabs.html' }) export class TabsPage { tab1Root = HomePage; tab2Root = AboutPage; tab3Root = ContactPage; constructor() { console.log('TabsPage'); } } |
Gore se može vidjeti da TabsPage komponenta kao tab1Root uzima HomePage komponentu i zato se ona učitava prije AppComponent.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import { Component } from '@angular/core'; import { NavController } from 'ionic-angular'; @Component({ selector: 'page-home', templateUrl: 'home.html' }) export class HomePage { constructor(public navCtrl: NavController) { console.log('HomePage'); } } |
Kako bi se to spriječilo potrebno je napraviti sljedeće:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
import { Component } from '@angular/core'; import { Platform } from 'ionic-angular'; import { StatusBar } from '@ionic-native/status-bar'; import { SplashScreen } from '@ionic-native/splash-screen'; import { TabsPage } from '../pages/tabs/tabs'; @Component({ templateUrl: 'app.html' }) export class MyApp { rootPage:any; provjera = 1; constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen) { platform.ready().then(() => { console.log('AppComponent'); // Okay, so the platform is ready and our plugins are available. // Here you can do any higher level native things you might need. statusBar.styleDefault(); splashScreen.hide(); if (this.provjera == 1) { this.rootPage = TabsPage; } else { // } }); } } |
Struktura projekta prema package.json
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
{ "name": "IonicTabsApp", "version": "0.0.1", "author": "Tomislav Stanković", "homepage": "https://www.tomislavstankovic.com/", "private": true, "scripts": { "clean": "ionic-app-scripts clean", "build": "ionic-app-scripts build", "lint": "ionic-app-scripts lint", "ionic:build": "ionic-app-scripts build", "ionic:serve": "ionic-app-scripts serve" }, "dependencies": { "@angular/animations": "5.2.9", "@angular/common": "5.2.9", "@angular/compiler": "5.2.9", "@angular/compiler-cli": "5.2.9", "@angular/core": "5.2.9", "@angular/forms": "5.2.9", "@angular/http": "5.2.9", "@angular/platform-browser": "5.2.9", "@angular/platform-browser-dynamic": "5.2.9", "@ionic-native/core": "4.6.0", "@ionic-native/splash-screen": "4.6.0", "@ionic-native/status-bar": "4.6.0", "@ionic/storage": "2.1.3", "ionic-angular": "3.9.2", "ionicons": "3.0.0", "rxjs": "5.5.8", "sw-toolbox": "3.6.0", "zone.js": "0.8.20" }, "devDependencies": { "@ionic/app-scripts": "3.1.8", "typescript": "~2.6.2" }, "description": "Ionic Project - IonicTabsApp" } |


























