im trying to make tabs in my HomePage, but the page isnt loading when i put the HomePage as the selected index in tabs, here's my Code:
home.ts:
...
export class HomePage {
public isConnected: Boolean= true;
homeRoot = HomePage;
aRoot = SigninPage;
bRoot = SignupPage;
...
home.html:
///SOME CONTENT
<ion-tabs selectedIndex="0" *ngIf="isConnected">
<ion-tab [root]="homeRoot" tabTitle="Home"></ion-tab>
<ion-tab [root]="aRoot" tabTitle="Home"></ion-tab>
<ion-tab [root]="bRoot" tabTitle="Home"></ion-tab>
</ion-tabs>
app.compenents.ts:
//IMPORTS
#Component({
templateUrl: 'app.html'
})
export class MyApp {
rootPage:any = HomePage;
constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen) {
platform.ready().then(() => {
// 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();
});
}
}
Create a new page TabsPage.
//Tabs.ts
export class TabsPage{
public isConnected: Boolean= true;
homeRoot = HomePage;
aRoot = SigninPage;
bRoot = SignupPage;
//Tabs.html
<ion-tabs selectedIndex="0" *ngIf="isConnected">
<ion-tab [root]="homeRoot" tabTitle="Home"></ion-tab>
<ion-tab [root]="aRoot" tabTitle="Home"></ion-tab>
<ion-tab [root]="bRoot" tabTitle="Home"></ion-tab>
</ion-tabs>
//app.component.ts
//IMPORTS
#Component({
templateUrl: 'app.html'
})
export class MyApp {
rootPage:any = TabsPage;
constructor(platform: Platform, statusBar: StatusBar, splashScreen:
SplashScreen) {
platform.ready().then(() => {
// 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();
});
}
}
Related
i want my App to check if a variable "myAccount" is on native storage to show or not Tabs, i used ionViewCanEnter to check if the variable myAccount is present on local Storage, if its not isConnected will be set to false, if its isConnected will be set to true
here's my code:
tabs.ts:
import { Component } from '#angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { HomePage } from '../home/home';
import { NativeStorage } from '#ionic-native/native-storage';
import { MyaccountPage } from '../myaccount/myaccount';
/**
* Generated class for the TabsPage page.
*
* See https://ionicframework.com/docs/components/#navigation for more info on
* Ionic pages and navigation.
*/
#IonicPage()
#Component({
selector: 'page-tabs',
templateUrl: 'tabs.html',
})
export class TabsPage {
public isConnected: Boolean= true;
homeRoot = HomePage;
myAccountRoot = MyaccountPage;
rootPage = HomePage;
constructor(public navCtrl: NavController, public navParams: NavParams, private nativeStorage: NativeStorage) {
}
ionViewCanEnter(){
this.nativeStorage.getItem('myAccount')
.then(
//data => this.isConnected=false,
// error => this.isConnected=true
data => this.isConnected=true,
error => this.isConnected=false
);
}
logout(){
this.nativeStorage.remove('myAccount');
this.isConnected=false;
}
}
if isConnected is true it will show the tabs with HomePage as selectedIndex with the tabs visible, if its not it will show the HomePage with no Tabs
tabs.html:
<ion-tabs selectedIndex="0" *ngIf="isConnected" class="tabs-icon-top tabs-color-active-positive">
<ion-tab [root]="homeRoot" tabIcon="home"> </ion-tab>
<ion-tab [root]="myAccountRoot" tabIcon="person"></ion-tab>
<ion-tab [root]="myAccountRoot" tabIcon="chatbubbles"></ion-tab>
<ion-tab (ionSelect)="logout()" tabIcon="power"></ion-tab>
</ion-tabs>
<ion-nav [root]="rootPage" *ngIf="!isConnected">{{isConnected}}</ion-nav>
The probleme is that its not totally working, when myAccount is added to storage, the tabs doesnt show up but if i exit the application and relaunch it, they appear
used ionViewWillEnter and it worked
I'm trying to create Ionic-3 Tab option, it's working fine, but my issue is I don't want to display 1st tab in tab menu,but I want to display 1st tab menu in page details in 1st time page opening ,I'm try to hide this 1st tab , but its not working for me, anyone knows how to do that?I have attached some images on my issue to help you understand it.
Tabs.html
<ion-tabs>
<ion-tab [root]="tab0Root"></ion-tab>
<ion-tab [root]="tab1Root" tabTitle="Check-In" tabIcon="people"></ion-tab>
<ion-tab [root]="tab2Root" tabTitle="Observations" tabIcon="information-circle"></ion-tab>
<ion-tab [root]="tab3Root" tabTitle="Activities" tabIcon="book"></ion-tab>
<ion-tab [root]="tab4Root" tabTitle="Health" tabIcon="medkit"></ion-tab>
</ion-tabs>
Tabs.ts
import { Component } from '#angular/core';
import { AboutPage } from '../about/about';
import { ContactPage } from '../contact/contact';
import { HomePage } from '../home/home';
import {HealthPage} from '../health/health';
import {MainPage} from '../main/main';
#Component({
templateUrl: 'tabs.html'
})
export class TabsPage {
tab0Root = MainPage;
tab1Root = HomePage;
tab2Root = AboutPage;
tab3Root = ContactPage;
tab4Root = HealthPage;
constructor() {
}
}
Every Tab has it own show property. So just change it to false if you want to hide it.
In tabs.ts:
import { Component, ViewChild } from '#angular/core';
import { Tabs } from 'ionic-angular';
#ViewChild(Tabs) tabs: Tabs;
ionViewDidEnter(){
this.tabs.getByIndex(0).show = false;
}
code side:
tab0Root = MainPage;
public isVisibleFirstTab : boolean = false;
mark-up:
<ion-tab *ngIf="isVisibleFirstTab" [root]="tab0Root"></ion-tab>
or
[rootParams] could be used for *ngIf condition
I have an app running on ionic 2, but I upgraded it to ionic 3 and after upgrading, some ionic plugins do not work, as is the case with the LoadingController and even the Platform.
generating the error "Cannot find name 'LoadingController'".
I looked at the ionic doc and did not figure out how to import them now
Follow my code:
import { LoadingController, Platform } from 'ionic-angular'
#Component({
selector: 'app-root',
templateUrl: './app.component.html',
providers: [ ProfileService, AlertService ]
})
export class AppComponent {
constructor(private router: Router,
public loadingCtrl: LoadingController,
private activatedRoute: ActivatedRoute,
private loginService: LoginService,
private profileService: ProfileService,
private _alertService: AlertService,
private platform: Platform ) {
router.events.subscribe((data) => {
this.path = data.url.substr(1);
})
this.getUserName();
this.platform.ready().then(() => {
this.platform.registerBackButtonAction(() => {
history.go(-1)
});
});
}}
LoadingController
ionic 3 loadingController link
home.html
<ion-header>
<ion-navbar>
<ion-title>Loading</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<button ion-button (click)="loading()">Show Loading</button>
</ion-content>
home.ts
import { LoadingController } from 'ionic-angular';
export class LoadingPage {
constructor(public navCtrl: NavController,public loadingCtrl: LoadingController) {
}
loading(){
let load = this.loadingCtrl.create({
content:'Please Wait....',
duration: 3000
});
load.present();
}
}
Ionic 2 seems very different from 1. I used the starter project and those are my files:
Tabs.html
<ion-tabs>
<ion-tab [root]="tab1Root" tabTitle="Home" tabIcon="home"></ion-tab>
</ion-tabs>
Tabs.ts
import { Component } from '#angular/core';
import { SigninPage } from '../Auth/Signin/Signin';
#Component({
templateUrl: 'Tabs.html'
})
export class TabsPage {
tab1Root: any = SigninPage;
constructor() {
}
}
app.components.ts
import { Component } from '#angular/core';
import { Platform } from 'ionic-angular';
import { StatusBar } from 'ionic-native';
import { TabsPage } from '../pages/Tabs/Tabs';
#Component({
template: `<ion-nav [root]="rootPage"></ion-nav>`
})
export class MyApp {
rootPage = TabsPage;
constructor(platform: Platform) {
platform.ready().then(() => {
StatusBar.styleDefault();
});
}
}
Is there any way to show a different tab with an if condition? For example, I want to show Tabs.App.html or Tabs.Auth.html depending on an if block.
What is the best way to do it with Ionic 2?
In ionic2 every page is accompanied by it's .ts file. So if you want 2 tabs, app.html and auth.html you would need to create both these pages and their .ts file.
In that way you can write an if-statement saying:
if(1 > 2){
this.rootpage = AuthTab;
}else{
this.rootpage = AppTab;
}
ps don't forget to import your components
I'm starting out on creating my first app using Ionic 2 and through a lot of trial and error have got to a point where no number of Google searching can find anything to help.
I'm trying to pass some NavParams to a tab. The NavParams are available in the parent tabs page:
#Page({
templateUrl: 'build/pages/tabs/tabs.html'
})
export class TabsPage {
constructor(params: NavParams) {
this.params = params;
console.log(this.params); // returns NavParams {data: Object}
// this tells the tabs component which Pages should be each tab's root Page
this.tab1Root = Tab1;
this.tab2Root = Tab2;
this.tab3Root = Tab3;
}
}
But I cannot seem to get the NavParams within a tab itself:
#Page({
templateUrl: 'build/pages/tab1/tab1.html'
})
export class Tab1 {
constructor(nav: NavController, params: NavParams, platform: Platform) {
this.nav = nav;
this.params = params;
this.platform = platform;
console.log(this.params); // returns NavParams {data: null}
}
}
I'm just not entirely sure how to pass the params from the tabs page to the tab itself or somehow request a param from the tab parent. I assume something like:
this.tab1Root = Tab1(this.params);
Any help would be greatly appreciated!
This question is a few weeks old so you may have already found the answer. This feature was added in February: https://github.com/driftyco/ionic/issues/5475.
Taking the code from your original tab page, let's say a parameter is passed to the "parent" tab page and we want to store it in a property called fooId (this could be an object, or just a simple integer or string value, whatever):
#Component({
templateUrl: 'tabs.html'
})
export class TabsPage {
constructor(params: NavParams) {
this.params = params;
console.log(this.params); // returns NavParams {data: Object}
this.fooId = this.params.data;
// this tells the tabs component which Pages should be each tab's root Page
this.tab1Root = Tab1;
this.tab2Root = Tab2;
this.tab3Root = Tab3;
}
}
Then in your tabs.html, you can reference it like this using the rootParams attribute (rootParams is referenced in the documenation here):
<ion-tabs>
<ion-tab tabTitle="Tab1" [root]="tab1Root" [rootParams]="fooId"></ion-tab>
<ion-tab tabTitle="Tab2" [root]="tab2Root" [rootParams]="fooId"></ion-tab>
<ion-tab tabTitle="Tab3" [root]="tab3Root" [rootParams]="fooId"></ion-tab>
</ion-tabs>
Then in your Tab1 page, you can reference your NavParams just like any other page and the value passed for foodId will be there.
For those who haven't figured out yet .....
I've searched a lot and the only answer I got was using native storage or using a service or session storage...but that wasn't what I wanted ...
So, If you have data in NavParams in Tabs.ts page and want to it pass as [rootParam] to respective Tabs...
then what you need to do is instead of assigning NavParams to a variable in Tabs.ts page what you can do is bind it directly to the [rootParams] in the HTML page.
Like ..
tabs.ts
constructor(public navParams: NavParams) { }
tabs.html
<ion-tab [root]="tab1Root" [rootParams]="navParams.get('single')" tabTitle="Customer"></ion-tab>
<ion-tab [root]="tab2Root" [rootParams]="navParams.get('second')" tabTitle="Map"></ion-tab>
Or
tabs.html
<ion-tab [root]="tab1Root" [rootParams]="navParams.data" tabTitle="Customer"></ion-tab>
tab1.ts
constructor( public navParams: NavParams) {}
ionViewDidLoad() {
console.log('ionViewDidLoad tab1Page');
console.log(this.navParams.data);
}
There is no direct way to pass Params in Tab-Pages that I know of.
I think you could play around with the NavController to make it work.
A neat workaround is to put the parameter into an injected service:
app/services/params.js:
import {Injectable} from 'angular2/core';
#Injectable()
export class Params{
constructor(){
console.log("Params()");
this.params={};
}
}
and in the controller:
import {Params} from '../../services/params'
#Page({
templateUrl: 'build/pages/home/home.html',
})
export class XYPage{
constructor(nav: NavController,platform: Platform, params: Params) {
console.log("XYPage()",this);
console.log("XYPage()", params.params);
params.params={id="001"};
dont forget to inject the service in your #App
I tried with many methods but none of them were helpful to me. As my application did not involve much of complexities, I implemented it using ionic native storage plugin. In the event that triggers the new tab page, I will store some variable in native storage(snippet is as below).
this.nativeStorage.setItem('myitem', {property: 'value', anotherProperty: 'anotherValue'})
.then(
() => console.log('Stored item!'),
error => console.error('Error storing item', error)
);
In the constructor or ionviewdidload page of the next tab page I will check for this variable and perform the required actions.Snippet is as below.
this.nativeStorage.getItem('myitem')
.then(
data => console.log(data),
error => console.error(error)
);
Note: This is suitable only for small application or where there is a requirement of passing limited set of variables. If there are lots of variable, it may take more space in the apps cache which may reduce the app performance.
tabs.html - add [rootParams]="paramName"; to the tab you want to pass data to
<ion-tabs>
<ion-tab tabTitle="Tab1" [root]="tab1Root" [rootParams]="fooId"></ion-tab>
<ion-tab tabTitle="Tab2" [root]="tab2Root"></ion-tab>
<ion-tab tabTitle="Tab3" [root]="tab3Root"></ion-tab>
</ion-tabs>
tabs.ts - set the key and data
export class TabsPage {
this.tab1Root = Tab1;
this.tab2Root = Tab2;
this.tab3Root = Tab3;
fooId: {
firstName: "lawlesscreation",
email: "user#email.com",
score: number // can also set it later in the constructor
}
constructor() {
let score = getScore(); //some method to get score
this.fooId.score = score;
}
}
tab1 page - import NavParams and use this.navParams.get(key) to get data
import { Component } from '#angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
#IonicPage()
#Component({
selector: 'page-profile',
templateUrl: 'profile.html',
})
export class Tab1 {
firstName = this.navParams.get('firstName');
score = this.navParams.get('score');
userEmail = this.navParams.get('email');
constructor(public navCtrl: NavController, public navParams: NavParams) {
}
}