harendra singh mahour
harendra singh mahour

Reputation: 74

Angular - trying to use deeplinks but only the app is opening, routing not working

I'm trying to use deeplinks but only the app is opening. The routing is not working.

Here is my code:

import { Component, ViewChild } from ‘@angular/core’;
import { App, Platform, Nav, NavController } from ‘ionic-angular’;
import { StatusBar } from ‘@ionic-native/status-bar’;
import { SplashScreen } from ‘@ionic-native/splash-screen’;
import { Deeplinks } from ‘@ionic-native/deeplinks’;
import { HomePage } from ‘…/pages/home/home’;
import { HarryPage } from ‘…/pages/harry/harry’;

@Component({
    templateUrl: ‘app.html’
})

export class MyApp {
    @ViewChild(‘myNav’) nav: Nav;
    rootPage:any = HomePage;

    constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen,private deeplinks: Deeplinks) {
        platform.ready().then(() => {
            statusBar.styleDefault();
            splashScreen.hide();
        });
        this.deeplinks.routeWithNavController( this.nav, {
            ‘/harry’: HarryPage,
            ‘/’ :HomePage
        }).subscribe( (match)=>{
                console.log(match.$route);
                // alert(JSON.stringify(match))
            }, (noMatch)=>{
                // alert(JSON.stringify(noMatch));
            })
    }
}

Whenever I try to access the /harry route, only the home page is opening.

Upvotes: 2

Views: 1178

Answers (2)

harendra singh mahour
harendra singh mahour

Reputation: 74

its working if you put same code inside root-page, if you follow their documentations then nothing going to work.

Upvotes: 2

Sudarshana Dayananda
Sudarshana Dayananda

Reputation: 5265

Try deeplinks logic inside platform.ready() callback.

      @ViewChild(Nav) nav:Nav;

      constructor(private deeplinks: Deeplinks) {

        platform.ready().then(() => {

         this.deeplinks.routeWithNavController(this.nav, {
            '/harry': HarryPage,
            '/' : HomePage

          }).subscribe(match => {

            console.log('Successfully matched route', JSON.stringify(match, null, 2));
          }, nomatch => {

            console.error('Got a deeplink that didn\'t match', nomatch);
          });
        });  
 }

Upvotes: 2

Related Questions