简体   繁体   English

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

[英]How to access the app.component variables and methods from other pages in Ionic?

I want to access and change app.component.ts variable or access methods from other pages ( otherpage.ts ) or other components such as;我想从其他页面( otherpage.ts )或其他组件访问和更改app.component.ts变量或访问方法,例如;

app.component.ts app.component.ts

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

  accessedVariable: any;

  constructor(){ }

  accessedMethod() {
   ..something
  }

}

otherpage.ts其他页面.ts

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

  constructor() { }
}

18-02-2020 18-02-2020

Please don't use Event emitter .请不要使用Event emitter Use the Observable pattern.使用Observable模式。 Otherwise, you'll have to face issues when updating to the Ionic 5 .否则,您将不得不在更新到Ionic 5时遇到问题。 ie no Event API on Ionic 5 .Ionic 5上没有事件 API。

Original原创

You can do this in a number of ways.您可以通过多种方式执行此操作。

One method where I can tell you is using Events .我可以告诉您的一种方法是使用Events

Events is a publish-subscribe style event system for sending and responding to application-level events across your app. Events 是一个发布订阅式事件系统,用于在您的应用程序中发送和响应应用程序级事件。

Another method may be using the provider .On that use case, you can share your methods and variables through the provider .另一种方法可能是使用provider 。在那个用例中,您可以通过provider共享您的methodsvariables

The fastest way is to use a GlobalVars Provider:最快的方法是使用GlobalVars提供程序:

install it first with:首先安装它:

ionic g provider globalvars

this will automatically add the new provider to your app.module.ts file这将自动将新的提供程序添加到您的app.module.ts文件中

import {Injectable} from '@angular/core';
@Injectable()
export class GlobalVars { 
myGlobalVar: any;
constructor() { 
this.myGlobalVar = ""; 
} 

setMyGlobalVar(value) { 
this.myGlobalVar = value; 
}

getMyGlobalVar() { 
return this.myGlobalVar; 
} 
}

You define there getter and setter methods and the needed variable can be accessed through an Instance of this class!您定义了gettersetter方法,并且可以通过此类的实例访问所需的变量! in YourOtherPage.ts you can get the variable with: this.glVars.getMyGlobalVar() for example.YourOtherPage.ts 中,您可以通过以下方式获取变量:例如this.glVars.getMyGlobalVar()

here you can read more about it: https://ionicallyspeaking.com/2016/03/10/global-variables-in-ionic-2/您可以在这里阅读更多相关信息: https : //ionicspeaking.com/2016/03/10/global-variables-in-ionic-2/

My use case Ionic 5 Capacitor: I wanted to show ion-spinner in ion-header of a header component.我的用例 Ionic 5 Capacitor:我想在头组件的 ion-header 中显示 ion-spinner。 In the header component, I subscribe to the observable variable which I can set true in another service.在标头组件中,我订阅了 observable 变量,我可以在另一个服务中将其设置为 true。 The approach is flexible and I can use it in child or parent components/pages.这种方法很灵活,我可以在子组件或父组件/页面中使用它。

MyService.service.ts: (initialises the observable variable and set method) MyService.service.ts:(初始化observable变量和set方法)

import { BehaviorSubject } from 'rxjs';

export class MyService {

    spinnerBehavior = new BehaviorSubject(false);
    obSpinner: any = this.spinnerBehavior.asObservable();

    set spinner(showSpinner: boolean) {
        this.spinnerBehavior.next(showSpinner);
    }
}

AnotherService.service.ts:另一个Service.service.ts:

export class AnotherService {

    constructor(private myService: MyService) {}

    public someMethod() {    
        this.myService.spinner = true;
    }
}

MyComponent.component.ts: MyComponent.component.ts:

export class MyComponent implements OnInit {
    public showSpinner: boolean;

    constructor(private myService: MyService) {}

    ngOnInit() {
        this.myService.obSpinner.subscribe(showSpinner => {
          this.showSpinner = showSpinner;
        });
      }
}

MyComponent.component.html: MyComponent.component.html:

<ion-spinner *ngIf="showSpinner"></ion-spinner>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM