在Angular 2中引用别名组件

时间:2017-03-23 10:38:39

标签: angular typescript ionic2

我在 Ionic 2 中遇到问题。我想通过将引用/别名添加到组件页面来推送新视图。

在我的父类(SettingsPage)上,我尝试推送一个新页面:

import * as Mapping from '[...]';

export class SettingsPage {
     public addon: Mapping.AddonMapModel;
     constructor() {
          this.addon = Mapping.AddonMap;
     }
}

父类(SettingsPage)TypeScript:

// Importing page component
import { MyComponentPage }  from '...';

export interface AddonMapModel {
    name: string,
    settingsLandingClss: any
}

export const AddonMap = { 
     name: 'Test', 
     settingsLandingClss: MyComponentPage // <--- Reference to component page
 }

我将页面组件存储为&#39; settingsLandingClss&#39;键:

@Override public boolean onPrepareOptionsMenu(Menu menu) {
    ...
    // SearchView doesn't retain it's state after orientation change
    // have to handle it the bad way (╯°□°)╯︵ ┻━┻
    boolean isQueryExists = !TextUtils.isEmpty(mSearchQuery);

    if (isQueryExists) {
        // Calling directly doesn't take effect
        // Custom runnable class in order to refrain from context leakage
        new Handler(Looper.getMainLooper()).post(new SearchMenuRunnable(mSearchView, mSearchQuery));
    }

    ...
}

注意:当我在父类上导入MyComponentPage时,它可以正常工作。

1 个答案:

答案 0 :(得分:1)

我通过名称实例化 MyComponentPage 解决了我的问题。

以下是一个示例(SettingsPage):

import * as SettingsLandingPages from '[...]';

addon.settingsLandingClss = SettingsLandingPages['nameofclass'];

对于SettingsLandingPages:

// Exporting all landing settings page

export * from './mycomponent.page';

有关详情和示例:Plunker

全部!