首页 > 其他分享 >在ionic2中自定义服务:该服务需要使用到NavController

在ionic2中自定义服务:该服务需要使用到NavController

时间:2022-11-28 23:00:23浏览次数:34  
标签:NavController service app ionic2 nav private constructor 自定义

       如果在自定义的服务中的constructor中直接注入(NavControlll) private navCtrl: NavController,然后再page页面或组件中的constructor中也采用注入的方式,可能会出现这样的错误:

No provider for NavController! (YourService -> NavController)

那该如何是好呢?

简单的方法有:在page中直接传递navCtrl对象到Service或者Provider中的constructor(此时使用该服务就不能使用注入的方式)或具体方法中(作为方法的参数),然后通过该对象使用。

另一种方案呢?

既保留服务能直接在构造方法中直接注入,又能使用navController对象。

But keep in mind, this is not good practice– services should not be tied to the UI of your application.(这话需要特别注意)

参考:

Ionic 2 UI/Alert from a Angular 2 Service

Ionic 2 use NavController in Service to show Alerts

Ionic2, inject NavController to Injectable Service

Ionic 2 UI/Alert from a Angular 2 Service

When you create an Ionic 2 page/component your constructor can inject the NavController like this:

constructor(private nav:NavController) {}

However, if you have tried this from a service then you’ve probably found this error:

No provider for NavController! (YourService -> NavController)

The particular use case i was tackling is to provide a simple Alert when there is an error in my service that was using Angular 2’s http.

To fix the problem, first import IonicApp:

import {IonicApp, Alert} from 'ionic-angular';

In our constructor we inject our app:

constructor(private app: IonicApp) {}

Now to get the NavController and present some UI we can:

let alert = Alert.create({title: 'Error', message: message, buttons: ['OK']}); var nav = this.app.getActiveNav(); nav.present(alert);

Hope this helps someone out struggling with the same problem.

Ionic 2 use NavController in Service to show Alerts

June 20, 2016  Uncategorized​  ​ 3 Comments  Can Kattwinkel

Usually you inject Nav:NavController into your classes constructor to use it:

But if you are working in a service you will receive the following error:


1



No provider for NavController ! ( ExampleService -> NavController )


If you want to show alerts from within a service you are required to use the NavController.  To do so simply create the private field nav with type NavController. Then use the app.getActiveNav method provided by ionic-angular. See the snippet below for a full example:


1



2



3



4



5



6



7



8



9



10



11



12



13



14



15



16



17



18



19



import { Injectable } from '@angular/core' ;



import { NavController , App } from "ionic-angular/index" ;



import { SetDetailPage } from "../../pages/set-detail-page" ;



 



@ Injectable ( )



export class ExampleService {



 



     private nav : NavController ;



 



     constructor ( private app : App ) {



         this . nav = app . getActiveNav ( ) ;



     }



 



     showAlert ( ) {



         this . nav . present ( {



             . . .



         } )



     }



}


Now you can show dialogs and alerts from a service in Ionic 2. But keep in mind, this is not good practice– services should not be tied to the UI of your application.

As you can se here, @mhartington (from ionic team) says:

Just to chime in on this, you shouldn't be injecting ViewController or NavController into Service. This is not their intended purpose.

And

The service shouldn't be responsible for displaying alerts/loading/ or any component that needs to be activated by nav. A service should just be for getting and returning data.

Anything else should be done within the component.

That being said, you can obtain the nav by doing

var nav = this.app.getActiveNav();

Like you can see here.

=================================================

EDIT:

It's bad practice to change a view from a service (broken MVC). However, you could send events from services to the main controller, and the controller can use NavController (best way), or you could send NavController to your service like an attribute (not bad way...). Or you may need to create a component instead of using the service.

So, a better way to do it, would be:

observable​ in your service, to know when the dismiss

import {Injectable} from '@angular/core';
import {Platform} from 'ionic-angular';
import {Observable} from 'rxjs/Observable';

@Injectable()
export class MyCustomService {

// Observables we're going to use
private dismissObserver: any;
public dismiss: any;

constructor(private platform: Platform){
// Your stuff
// ...

this.dismissObserver = null;
this.dismiss = Observable.create(observer => {
this.dismissObserver = observer;
});
}

public yourMethod(...):void {
// Here we send the order to go back to the home page
this.dismissObserver.next(true);
}
}

only, in your app.ts

initializeApp(): void {
this.platform.ready().then(() => {
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
StatusBar.styleDefault();

// We subscribe to the dismiss observable of the service
this.myCustomService.dismiss.subscribe((value) => {
this.navController.setRoot(HomePage);
});
});
}

ionicBootstrap

ionicBootstrap(MyApp, [MyCustomService, ...], {
//statusbarPadding: true
});

Or, following the Angular2 Style Guide​, add it as a provider

@Component({
templateUrl: 'build/app.html',
directives: [...],
providers: [MyCustomService]
})
class MyApp {
// ...
}

标签:NavController,service,app,ionic2,nav,private,constructor,自定义
From: https://blog.51cto.com/u_15894233/5893784

相关文章

  • ionic2中ThemeableBrowser插件的使用——App内嵌浏览器
    参考:​​ThemeableBrowser​​    需要注意的是低版本的ionic2项目可能无法使用该插件,尽管可以根据官方给出的成功下载好插件,在使用的过程中也会发现不可用的情况:Th......
  • 什么?在ionic2上实现支付宝支付
    参考:​​cordova-plugin-alipay——ionic2支付宝App支付插件,支持Android、iOS​​    在ionic2上实现支付宝支付的功能,需要使用到一个封装好的cordova插件(当然,如果......
  • ionic2实现社会化分享Social Sharing
    SocialSharing插件:​​SocialSharing​​参考:​​phonegap/cordova开发中的分享功能​​    在ionic2项目下,执行如下语句安装插件:$ionicpluginaddcordova-plug......
  • 这回又来个 ionic2 的微信支付!(遇到一些莫名的BUG)
    目录​​目录​​​​ionic2实现微信支付前的准备工作​​​​ionic2中的微信支付​​​ionic2中实现微信支付的参考代码​​客户端APP​​​实现插件的封装WechatPlugints......
  • 我打包发布ionic2 APP正式版的步骤
    目录​​目录​​​​Android平台的apk打包​​​​iOS平台生成构建版本的注意事项​​​Android平台的具体参考​​​Publishingyourapp​​这里进行记录,方便查找:Androi......
  • Android自定义搜索框(EditText)的搜索功能实现,过滤ListView
    实现自定义搜索框(实际上就是一个EditText):<RelativeLayoutandroid:id="@+id/music_search_rl"android:layout_width="match_parent"android:layout_......
  • ionic2 中的网络监测功能
    参考:​​http://ionicframework.com/docs/v2/native/network/​​(后须还将发表其他我正在ionic2使用的插件或功能)增加网络监测功能的cordova插件:$ionicpluginaddcordova......
  • ionic2上实现跳转应用市场的功能
        需要使用到如下插件:(参考官网的ionic-native)​​AppVersion​​ 主要用于获取Appde包名:AppVersion.getPackageName();​​WebIntent​​用于在Android平台上调......
  • Ionic2中的相册选择和拍照上传——ImgService
    目录​​目录​​​​前言​​​​Cordova准备​​​​ImgService服务的实现​​​​ImgService服务的使用​​​​示例效果​​​​相册选择器的汉化​​​​参考​​前言......
  • JSP中的自定义标签
    目录​​目录​​​​简介​​​​入门案例​​​​自定义标签功能扩展​​​​传统自定义标签的运行原理​​​传统自定义标签的使用​​​控制JSP页面部分内容执行​​​......