如何在提供程序内部使用NavController-Ionic

时间:2018-10-24 17:21:11

标签: ionic-framework ionic3 angularjs-service

我为ionic应用程序创建了通用提供程序,我想验证用户是否登录 所以我创建了一个函数来检查令牌是否存在,但是在验证令牌是否为空之后,我想通过setRoot页面登录或注册页面,所以我使用了navcontroller来设置Root Page,但是在运行服务器后它显示了错误

错误

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 (提供商文件)

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

3 个答案:

答案 0 :(得分:1)

尝试添加此行代码
import { NavController } from 'ionic-angular';

答案 1 :(得分:1)

您不应在提供程序中使用navController。它应该在相应页面/组件的.ts文件中。

但是,如果您仍要使用它,则忘记了将navController导入您的提供程序中。

添加

  

从“ ionic-angular”导入{NavController};

位于顶部,您就可以开始

答案 2 :(得分:0)

您可以执行以下操作:

import { App } from 'ionic-angular';

@Injectable()
export class YourProvider {

  constructor(private app: App) {}

  yourVerifyFunction() {
    this.app.getRootNav().setRoot('YourPage');
  }
}

欢呼