我使用以下命令創建了一個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');
}
}