2017-01-03 85 views

回答

2

myHelperFunction.ts

export function myLittleBoy(){ 
    console.log('I see you) 
} 

RegisterComponent.ts

import {myLittleBoy} from 'path to myhelperfunction.ts' 
1
export function myLittleBoy(){ 
    console.log('I see you) 
} 



import { Component } from '@angular/core'; 
declare var myLittleBoy:any; 
@Component({ 
    selector: 'my-app', 
    template: '<h1>Hello {{name}}</h1>', 
}) 
export class AppComponent { 

    constructor(private __jsmodel:JSmodelEvents){ 
     myLittleBoy(); 

    } 

} 
0
import {myHelperNumber} from 'your path'; 
export class AppComponent{ 
    constructor(private helpNum: myHelperNumber){ 
     // Now you can use myLittleBoy function anywhere inside the AppComponent Class 
     // Like below shown: 
     // this.helpNum.myLittleBoy(); 
    } 
} 

确保您myHelperNumber文件是导出的格式。

0

myHelperNumber.ts:

export const function myLittleBoy(){ 
    console.log('I see you) 
} 

然后在User.ts或userService.ts

import {myLittleBoy} from './myHelperNumber'; 
// and just use it in this class 
+0

这样做,它说myHelperNumber是决心路径喇嘛喇嘛......但--alowJs没有设置 –

+0

当然,使用'.TS '作为文件结尾而不是js。 –