2017-03-08 35 views
-1

我想用一個像Array.prototype.x(...)這樣的函數原型的文件,但我不知道如何將它包含爲全局。如何在離子2上包含原型腳本?

例子:

Array.ts

Array.prototype.x = function(i) {return this[i]} 
Array.prototype.y =() => {return true} 
Array.prototype.z =() => {return true} 

頁/家庭/ home.ts

import { Component } from '@angular/core'; 
@Component({ 
    selector: 'page-home', 
    templateUrl: 'home.html' 
}) 
export class HomePage { 
    constructor() {} 

    ionViewDidLoad() { 
     let a [1,2,3,4,5,6,7,8,9,0]; 
     console.log(a.x()); // see Array.prototype.x() 
     console.log(a.y()); // see Array.prototype.y() 
     console.log(a.z()); // see Array.prototype.z() 
    } 
} 

PS:我的原型文件有太多的功能

+1

就包括它的頭,它會工作。 – dfsq

+0

那麼,我的「Array.ts」功能太多了...... –

回答

1

剛剛導入的文件:

import { Component } from '@angular/core'; 
import './Array'; 
@Component({ 
    selector: 'page-home', 
    templateUrl: 'home.html' 
}) 
export class HomePage { 
    constructor() {} 

    ionViewDidLoad() { 
     let a [1,2,3,4,5,6,7,8,9,0]; 
     console.log(a.x()); // see Array.prototype.x() 
     console.log(a.y()); // see Array.prototype.y() 
     console.log(a.z()); // see Array.prototype.z() 
    } 
} 
+0

非常感謝! –

+0

@OlafErlandsen歡迎您。 –