Ionic 2/Angular 2 中的全局函数 [英] global function in Ionic 2 / Angular 2

查看:12
本文介绍了Ionic 2/Angular 2 中的全局函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I setup a global function that can be accessed throughout all views?

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

In app.component.ts I added a simple method

  openShare() {console.log("button clicked")} 

然后在我的导航中我有

  <button ion-button right (click)="openShare()">
  <ion-icon name="md-share"></ion-icon>
</button>

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

When I try to access this from any page I get the following.

self.context.openShare is not a function

但是,如果我将它直接放在构造函数中(例如 this.openShare(); ),它确实执行得很好,但是当使用 (click) 函数从任何页面调用时,它就不起作用了.

It does however execute fine if I put it directly in the constructor (e.g this.openShare(); ), but when calling from any page using a (click) function it just doesn't work.

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

Is app.component.ts not global? I thought this is where I would place it, but maybe I am missing something.

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

Basically its a function for a simple button on the nav, I need it to be global though since its used on every page.

任何帮助将不胜感激,仍在解决 Ionic 2 问题.

Any help would be appreciated, still figuring Ionic 2 out.

推荐答案

你可以使用Directive.

尝试如下.

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

Put the following code in separate directive file. (social-sharing.directive.ts);

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

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

  constructor() { }

  @HostListener('click') onClick() {
    // Your click functionality
  }
}

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

Import it into app.module.ts and than add to declarations.

在 HTML 中,只需将 attribute 添加到指令文件中作为 selector 的任何元素.

In HTML, just add attribute to any element which is the selector in your directive file.

<button ion-button right socialSharing>
 <ion-icon name="md-share"></ion-icon>
</button>

其他信息:

将值从组件传递到指令.

Passing values from component to directive.

home.html

  <button ion-button right [socialSharing]="property">
    <ion-icon name="md-share"></ion-icon>
  </button>

home.component.ts

 export class HomePage {

   property: string = 'some url';
    constructor() {}
 }

social-sharing.directive.ts

@angular/core 导入 Input 和其他内容.

Import Input along with others from @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);
  };
}

在指令中使用元素:

social-sharing.directive.ts

@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);        
   };
 }

这篇关于Ionic 2/Angular 2 中的全局函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆