ionic Uncaught (in promise): Error: StaticInjectorError[NavController]

Error: Uncaught (in promise): Error: StaticInjectorError[NavController]
By default you can’t use navController or viewController or anything like that in a Controller or a service.
Code:


An easier solution,

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { NavController, AlertController, App } from 'ionic-angular';
import {LoginPage} from "../pages/login/login";
@Injectable()
export class CommonProvider {

    public session:any;
    constructor(
        public http: HttpClient, 
        public alertCtrl: AlertController, 
        public appCtrl : App
    ) {}

    public getSession() {
        let activeNav: NavController = this.appCtrl.getActiveNav();
        this.session = window.localStorage.getItem('session');
        if(!this.session) {
            activeNav.setRoot(LoginPage);
            return false;
        }
        return this.session;
    }

Reference: https://www.cnblogs.com/wupeng88/p/8340947.html

Read More: