Ionic 2/Angular 2中的全局函数

lim*_*mit 9 typescript ionic2 ionic3 angular

如何设置可在所有视图中访问的全局函数?

在app.component.ts中我添加了一个简单的方法

  openShare() {console.log("button clicked")} 
Run Code Online (Sandbox Code Playgroud)

然后在我的导航中我有

  <button ion-button right (click)="openShare()">
  <ion-icon name="md-share"></ion-icon>
</button>
Run Code Online (Sandbox Code Playgroud)

当我尝试从任何页面访问它时,我得到以下内容.

self.context.openShare is not a function
Run Code Online (Sandbox Code Playgroud)

但是,如果我将它直接放在构造函数中(例如this.openShare();),它确实执行正常,但是当使用(单击)函数从任何页面调用时它只是不起作用.

app.component.ts不是全局的吗?我认为这是我放置的地方,但也许我错过了一些东西.

基本上它是导航器上一个简单按钮的功能,我需要它是全局的,因为它在每个页面上使用.

任何帮助将不胜感激,仍然可以计算Ionic 2.

Har*_*ish 11

你可以用Directive.

尝试如下.

将以下代码放在单独的指令文件中.(社会sharing.directive.ts);

import { Directive, HostListener } from '@angular/core';

@Directive({
  selector: '[socialSharing]'
})
export class SocialSharing {

  constructor() { }

  @HostListener('click') onClick() {
    // Your click functionality
  }
}
Run Code Online (Sandbox Code Playgroud)

将其导入app.module.ts并添加到declarations.

在HTML中,只需添加attributeselector指令文件中的任何元素即可.

<button ion-button right socialSharing>
 <ion-icon name="md-share"></ion-icon>
</button>
Run Code Online (Sandbox Code Playgroud)

附加信息:

将值从组件传递到指令.

home.html的

  <button ion-button right [socialSharing]="property">
    <ion-icon name="md-share"></ion-icon>
  </button>
Run Code Online (Sandbox Code Playgroud)

home.component.ts

 export class HomePage {

   property: string = 'some url';
    constructor() {}
 }
Run Code Online (Sandbox Code Playgroud)

社会sharing.directive.ts

Input与其他人一起导入@angular/core.

import { Directive, Input, HostListener } from '@angular/core';

@Directive({
   selector: '[socialSharing]'
})

export class SocialSharing {
  @Input('socialSharing') str: string;

  constructor() {}

  @HostListener('click') onClick() {
     console.log(this.str);
  }
};

  ngAfterInit() {
     console.log(this.str);
  };
}
Run Code Online (Sandbox Code Playgroud)

在指令中使用元素:

社会sharing.directive.ts

ElementRef与其他人一起导入@angular/core

 import { Directive, ElementRef, HostListener } from '@angular/core';

 @Directive({
   selector: '[socialSharing]'
 })

 export class SocialSharing {

   // Add ElementRef to constructor

   constructor(el: ElementRef) {};

   ngOnInit() {
      let elRef = this.el.nativeElement;
      console.log(elRef.innerHTML);        
   };
 }
Run Code Online (Sandbox Code Playgroud)


Sam*_*ath 8

您可以轻松地使用Providers.当您需要使用该功能(或提供程序)时,您只需将inject它放入相关组件中即可.就是这样.

方法1:

您可以使用CLI创建提供程序.

> ionic g provider YourProvider
Run Code Online (Sandbox Code Playgroud)

您-provider.ts

 import { Injectable } from '@angular/core';

    @Injectable()
    export class YourProvider {

      constructor() {
      }

       openShare() {console.log("button clicked")} 
    }
Run Code Online (Sandbox Code Playgroud)

app.module.ts

import { YourProvider } from "../pages/path";

    @NgModule({
      declarations: [
        MyApp,
      ],
      imports: [
        IonicModule.forRoot(MyApp),
       ],
      bootstrap: [IonicApp],
      entryComponents: [
        MyApp,
        ],
      providers: [{ provide: ErrorHandler, useClass: IonicErrorHandler },YourProvider  ]
    })

    export class AppModule { }
Run Code Online (Sandbox Code Playgroud)

您-view.ts

       import { YourProvider } from '../../providers/your-provider';

        export class YourViewPage{

          constructor(public yourProvider : YourProvider) {

          }

        openShare(){
           this.yourProvider.openShare(); 
        }
Run Code Online (Sandbox Code Playgroud)

方法2:创建abstract基类.

我的碱基class.ts

export abstract class MyBaseClass {

  constructor() {
  }

  protected openShare():void {
    console.log("button clicked"); 
  }

}
Run Code Online (Sandbox Code Playgroud)

我-view.ts

export class MyViewPage extends MyBaseClass {
  constructor()
  {
     super();
  }

       openShare(){
               super.openShare(); 
            }
  }
Run Code Online (Sandbox Code Playgroud)