扩展Ionic2并注入ModalController [英] Extending Ionic2 and injecting ModalController

查看:194
本文介绍了扩展Ionic2并注入ModalController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发一个可用于任何项目的datepicker组件。

I am trying to develop a datepicker component that can be used for any project.

我有一个包含组件的NgModule,我将IonicModule注入其中,因此它可以使用ionic2的所有组件/指令。

I have an NgModule that has components and I inject IonicModule in to it so it can use all the components/directives of ionic2.

@NgModule({
    imports: [
        CommonModule,
        IonicModule.forRoot(DatePickerModule)
    ],
    exports: [DatePickerComponent, DatePickerDirective],
    entryComponents: [DatePickerComponent],
    declarations: [DatePickerComponent, DatePickerDirective],
    providers: [DateService, nls]
})
export class DatePickerModule {
    static forRoot(): ModuleWithProviders {
        return {
            ngModule: DatePickerModule
        };
    }
};

其中一个组件注入离子模态控制器以显示模态。

One of the components injects ionic modal controller to display a modal.

export class MainDirective{
  public static config:any;
    constructor(private modalCtrl:ModalController) {
    }
    openModal() {
        this.modalCtrl.create(DatePickerComponent
        ).present();
    }
}

此NgModule导入到另一个应用程序中并从中导入app我试图打电话给openModal。
这是我导入的方式

This NgModule is imported in to another App and from that app I am trying to call openModal. This is how I import

@NgModule({
  imports: [
    IonicModule.forRoot(App),
    DatePickerModule.forRoot(),
],
})

它会从离子角度库中抛出未定义的错误'_getPortal'。
我猜它找不到要显示的应用程序。

What happens is that it throws an error '_getPortal' of undefined from ionic-angular library. I am guessing that it can't find the app to display to.

我也猜测我需要传递给权限的实际应用程序工作,但我不知道如何做到这一点。

I am also guessing that I need to pass to forRoot the ACTUAL APP that will be working but I have no idea how to do this.

解决这个问题的最佳方法是什么?

What would be the best way to approach this problem?

推荐答案

这个解决方案是扩展离子中的特定组件,以表明我的组件是它自己的模态。

The solution for this was to extend specific components in ionic to indicate that my component was a modal of its own kind.

将模态控制器注入外部组件是太无效了,在我看来不应该这样做,因为你的组件不是模态。

Injecting the modalcontroller in to a foreign component is too ineffecient and shouldn't be done in my opinion since your component isn't a modal.

对于任何东西都是将要显示的ViewController应该被扩展,因为这是每次执行一个当前函数时显示的基类类型。

For anything that is going to be displayed ViewController should be extended since this is the type of baseclass that is being displayed everytime you execute a present function.

代理的当前函数是一个当前函数应用程序本身告诉它显示所述viewcontroller。

A present function by proxy is a present function the app itself telling it to display said viewcontroller.

所以要修改

扩展com ponent是一个viewcontroller:

Extend component to be a viewcontroller:

export class MyComponent extends ViewController

将应用程序注入构造函数

Inject the App in to your constructor

constructor(private _app:App){}

在应用上显示你的新组件

Display your new component on to the app

this.app.present(this);

现在可以显示模态。

这篇关于扩展Ionic2并注入ModalController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆