如何从Ionic中的其他页面访问app.component变量和方法?

Dr.*_*. X 3 typescript ionic-framework ionic3 angular

我想app.component.ts从其他页面(otherpage.ts)或其他组件访问和更改变量或访问方法,如;

app.component.ts

@Component({
  templateUrl: 'app.html'
})
export class MyApp {

  accessedVariable: any;

  constructor(){ }

  accessedMethod() {
   ..something
  }

}
Run Code Online (Sandbox Code Playgroud)

otherpage.ts

@Component({
  selector: 'page-other',
  templateUrl: './otherpage.html',
})
export class OtherPage {

  constructor() { }
}
Run Code Online (Sandbox Code Playgroud)

Sam*_*ath 7

您可以通过多种方式执行此操作.

我可以告诉你的一种方法是使用事件.

事件是一种发布 - 订阅样式事件系统,用于在整个应用程序中发送和响应应用程序级事件.

另一种方法可能是使用provider.On那个用例,你可以分享你的methodsvariables通过provider.