次のコマンドを使用してIonic v2アプリケーションを作成しました:Ionic 2にナビゲートされたページからメインコンポーネントの機能にアクセスするにはどうすればよいですか?
ionic start my-app sidemenu --v2 --ts
app.ts
ファイルの中には、いくつかのことを行うためのロジック(機能)があります(サイドメニューを表示するためのモーダルと状態の維持など)。特定のページ(例:pages/getting-started/getting-started.ts
)が表示されている場合は、同じ機能をapp.ts
に再利用したいと考えています。どのページに移動したらapp.ts
の機能にアクセスするには?
私のapp.ts
は次のようになります。
class MyApp {
@ViewChild(Nav) nav:Nav;
private rootPage:any = GettingStartedPage;
private pages:any;
constructor(platform:Platform) {
this.initializeApp();
this.pages = {
'GettingStartedPage': GettingStartedPage,
'AnotherPage': AnotherPage //more pages and modals
};
}
initializeApp() {
this.platform.ready().then(() => {
StatusBar.styleDefault();
});
}
openPage(page:string) {
//when a user clicks on the left menu items, a new page is navigated to
let component this.pages[page];
this.nav.setRoot(component);
}
openModal(page:string) {
//modals are opened here, there's more complicated logic
//but this serves to demonstrate my problem
let component = this.pages[page];
Modal.create(component);
}
}
ionicBootstrap(MyApp);
私のgetting-started.ts
は次のようになります。
export class GettingStartedPage {
constructor(
platform:Platform,
viewController:ViewController,
navController:NavController,
navParams:NavParams) {
}
buttonClicked() {
//i need to access app.ts openModal here
//how do i call a method on app.ts?
//like MyApp.openModal('SomeModal');
}
}