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

在服务(provider)中t引入NavController时出现错误:Uncaught (in promise): Error: StaticInjectorError[NavController]

默认情况下不能在controller 或者service中使用navcontroller或者viewcontroller之类的。

其中代码:

ionic Uncaught (in promise): Error: StaticInjectorError[NavController]_第1张图片

ionic Uncaught (in promise): Error: StaticInjectorError[NavController]_第2张图片

一个比较容易解决的方法,

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;
    }

参考:https://www.cnblogs.com/wupeng88/p/8340947.html


你可能感兴趣的:(Ionic,Angular)