0

I am just a beginner to ionic with some angular knowledge.I have tabs template, The Activity tab will have 3 tab-buttons on the page as shown in below image:

enter image description here

As shown in the image when the user click on:

  • about button, the user will be routed to about page
  • home button, the user will routed to home page
  • contact button, the user will routed to contact page

within tabs page . This scenario works fine.

Now i have another page called add-contact. When the user click on add-contact button in contact page he must be routed to add-contact page along with tabs-menu something like this:

enter image description here

While surfing i got this question. Here they are routing to other page along with the clicked object ID and displaying that object properties.

I don't want to perform such operation, I just want to route another page (i,e add-contact) as shown in the 2nd image.

Since pages are more, i am giving Stackblitz DEMO

Shankar
  • 2,565
  • 8
  • 29
  • 56

3 Answers3

3

NavController is an Ionic V3 navigation method

See https://ionicframework.com/docs/v3/api/components/tabs/Tab/ and https://ionicframework.com/docs/v3/api/navigation/NavController/ for details.

So inside each of the tab root pages you can use

import { NavController } from 'ionic-angular';

constructor(public navCtrl: NavController) {}

And in your method: this.navCtrl.push(NewPage) to navigate to a new page.

Here is the modified stackblitz:

https://stackblitz.com/edit/ionic-oykegj

Ionic V4 primarily uses Angular Routing

See: https://ionicframework.com/docs/navigation/angular

You'll have to declare routes then use [routerLink] to navigate. It's a little more work at the beginning, but quite powerful.

This tutorial runs through how to update your app and why: https://www.joshmorony.com/converting-ionic-3-push-pop-navigation-to-angular-routing-in-ionic-4/

iosepa
  • 381
  • 1
  • 8
  • Sorry for the late reply, I am getting this lint error: `Property 'push' does not exist on type 'NavController'` when i use in my app, My app version is ionic - 4. – Shankar May 10 '19 at 04:04
  • In `newContact` function i tried like this ===>`this.navCtrl.navigateRoot('/new-contact');`, It is routing but it is routing to separate page , not routing inside the `tab-menu`. – Shankar May 10 '19 at 04:16
  • My stackblitz is a forked version of yours. I only made changes in pages/contact/ and app.module.ts. Besides those three files it's exactly the same as yours. I'd look closely at the imports, make sure you are importing it correctly and declaring it in the constructor. The links say v3 but these features work with v4. The part you're interested is in the NavController page under Pushing a View. – iosepa May 10 '19 at 05:04
  • I have mad same changes you did in `app.module.ts` and in `contact` page, Since my app version 4, In contact page i have imported `NavController` like this ====> **import { NavController } from '@ionic/angular';** and created its instance in constructor like this: **constructor(public navCtrl: NavController) {}** Then in the `newContact` method i have written like this ===> **newContact(){ this.navCtrl.push(AddContactPage); }** – Shankar May 10 '19 at 06:04
  • I have imported `AddContactPage` in contact page as you imported in the DEMO. – Shankar May 10 '19 at 06:06
  • In my current all pages i,e `contact,home,about` all are under `app` folder there is no folder called `pages`. Does it make any issue? – Shankar May 10 '19 at 06:17
  • You're right! Looks like Ionic 4 is moving away from NavController. You should be able to use .goForward - https://stackoverflow.com/questions/51828017/navcontroller-doesnt-work-in-ionic-4 but the "better" way to do it would be to update to the angular router. It's not hard. I've got to go now, but can update my answer later. – iosepa May 10 '19 at 12:43
  • I have seen this question, In this i am able to `route` but the `tabs menu` will be hidden.It will route completely to `add-contact` page. – Shankar May 13 '19 at 08:55
  • Is your question answered now? I'm not sure what you're looking for. If you want to use the V3 methods - use the nav-controller from ionic-angular. – iosepa May 14 '19 at 02:17
  • My question is not answered, I don't want use v-3. I want the solution for v-4 – Shankar May 15 '19 at 03:58
2

Navigate to the TypeScript file for your 'Contact' page. In this file, you need to create a function that pushes the add-contact page when the 'ADD CONTACT' button is clicked.

Ensure that the NavController has been imported:

import { NavController } from 'ionic-angular';

Below the import, your code should look something like this:

export class ContactPage {

constructor(public navCtrl: NavController) {

}


addPageLink() {
this.navCtrl.push(addContactPage);
{



{

Now navigate back to the HTML page for your 'Contacts' page:

In the code for your 'ADD CONTACT' button, you will need to call the function you just created.

Your code should look similar to this:

<button ion-button (click)="addPageLink()">
ADD CONTACT
</button>

Now, when the button is clicked, you should be redirected to the 'add-contact' page.

I hope this helps, please let me know how you get on and of course let me know if you have any additional questions.

lewisodea
  • 21
  • 3
0

The module should be included inside the route's tabs module in order to navigate to another page within the tabs. Assuming you have three tabs (home, about and contact), to add a new route (add-contact) to the tabs navigation, you have to add the new module into tabs-routing.module.ts:

const routes: Routes = [
{
    path: '',
    component: TabsPage,
    children: [
      { path: 'home', loadChildren: () => import(...).then(m => m.HomePageModule) },
      { path: 'about', loadChildren: () => import(...).then(m => m.AboutPageModule) },
      { path: 'contact', loadChildren: () => import(...).then(m => m.ContactPageModule) },
      ...
      // set the 'add-contact' route inside the children of tabs route
      { path: 'add-contact', loadChildren: () => import(...).then( m => m.AddContactPageModule) },
    ]
}]

Your button inside Contact's page should now specifies the route with routerLink to navigate to as follows:

<ion-button [routerLink]="['/add-contact']" routerDirection="forward">Add</ion-button>

This way, the add-contact page is wrapped by the tabs.

Blo
  • 11,903
  • 5
  • 45
  • 99