Search code examples
ionic-frameworkionic3angularjs-service

How to use NavController inside a provider - Ionic


I have created a Common provider for the ionic app and i want to verify that user is logged in or not so i have created a function verify to check if token exists or not but after verifying that token is null i want to setRoot page to login or register page so i used navcontroller to set Root Page but after running server it shows error

Error

ERROR Error: Uncaught (in promise): Error: StaticInjectorError(AppModule)[Content -> NavController]: 
  StaticInjectorError(Platform: core)[Content -> NavController]: 
    NullInjectorError: No provider for NavController!
Error: StaticInjectorError(AppModule)[Content -> NavController]: 
  StaticInjectorError(Platform: core)[Content -> NavController]: 
    NullInjectorError: No provider for NavController!
    at _NullInjector.get (VM1014 vendor.js:1377)
    at resolveToken (VM1014 vendor.js:1675)
    at tryResolveToken (VM1014 vendor.js:1617)
    at StaticInjector.get (VM1014 vendor.js:1485)
    at resolveToken (VM1014 vendor.js:1675)
    at tryResolveToken (VM1014 vendor.js:1617)
    at StaticInjector.get (VM1014 vendor.js:1485)
    at resolveNgModuleDep (VM1014 vendor.js:11270)
    at _createClass (VM1014 vendor.js:11311)
    at _createProviderInstance$1 (VM1014 vendor.js:11281)
    at _NullInjector.get (VM1014 vendor.js:1377)
    at resolveToken (VM1014 vendor.js:1675)
    at tryResolveToken (VM1014 vendor.js:1617)
    at StaticInjector.get (VM1014 vendor.js:1485)
    at resolveToken (VM1014 vendor.js:1675)
    at tryResolveToken (VM1014 vendor.js:1617)
    at StaticInjector.get (VM1014 vendor.js:1485)
    at resolveNgModuleDep (VM1014 vendor.js:11270)
    at _createClass (VM1014 vendor.js:11311)
    at _createProviderInstance$1 (VM1014 vendor.js:11281)
    at c (VM1012 polyfills.js:3)
    at Object.reject (VM1012 polyfills.js:3)
    at NavControllerBase._fireError (VM1014 vendor.js:51258)
    at NavControllerBase._failed (VM1014 vendor.js:51251)
    at VM1014 vendor.js:51298
    at t.invoke (VM1012 polyfills.js:3)
    at Object.onInvoke (VM1014 vendor.js:5134)
    at t.invoke (VM1012 polyfills.js:3)
    at r.run (VM1012 polyfills.js:3)
    at VM1012 polyfills.js:3

common.ts (Provider File)

import { NavController } from 'ionic-angular';
import { Storage } from '@ionic/storage';
import { HttpClient } from '@angular/common/http';

@Injectable()
export class CommonProvider {
   base_url : string = "http://192.168.1.198:3000/v1/";
   constructor(public http: HttpClient,public storage:Storage,private 
   navctrl:NavController) {
      console.log('Hello CommonProvider Provider');
    }

  registerUser(values) {
     return this.http.post(this.base_url+'/signups', values);
   }

  loginUser(login) {
     return this.http.post(this.base_url+'/login', login);
   }

  verify(){
     this.storage.get('token').then((val)=>{
      if(val == null){
         this.navctrl.setRoot('LoginPage');
         this.storage.set('page','LoginPage');
       }
    });
  }
}

Solution

  • You can do something like this:

    import { App } from 'ionic-angular';
    
    @Injectable()
    export class YourProvider {
    
      constructor(private app: App) {}
    
      yourVerifyFunction() {
        this.app.getRootNav().setRoot('YourPage');
      }
    }
    

    Cheers