2016-09-25 4 views
1

Я хотел бы создать "Utility Модуль": tnt.module.ts:Angular2 2.0.0 Как добавить простые функции @NgModule

import {NgModule} from '@angular/core'; 
import {toUpperCase} from "./tnt.utils"; 

@NgModule({ 
    declarations: [ 
    toUpperCase 
    ], 
    exports: [ 
    toUpperCase 
    ] 
}) 
export default class TntModule {} 

Здесь вы один Util функции Пример: tnt.utils. ц

export const toUpperCase= function (str:String) { 
    return str.toUpperCase() 
} 

Я получаю ошибку:

ERROR in [default] /data/2016/le-tube/src/app/shared/tnt/tnt.module.ts:5:10 
Argument of type '{ imports: undefined[]; declarations: ((str: String) => string)[]; exports: ((str: String) => str...' is not assignable to parameter of type 'NgModule'. 
    Types of property 'declarations' are incompatible. 
    Type '((str: String) => string)[]' is not assignable to type '(any[] | Type<any>)[]'. 
     Type '(str: String) => string' is not assignable to type 'any[] | Type<any>'. 
     Type '(str: String) => string' is not assignable to type 'Type<any>'. 
      Type '(str: String) => string' provides no match for the signature 'new (...args: any[]): any' 

Что я упускаю? Почему я не могу создать модуль с простыми функциями? Я предполагаю, что я просто не хватает простой детали, но я не могу понять это ...

+0

Я не могу ответить на него из информации, предоставленной. Тем не менее, я могу посоветовать вам использовать угловые cli: https://github.com/angular/angular-cli и toUppercase мне кажется кандидатом в «трубу». Для функций вы должны использовать сервис. – user3791775

+0

Я использую angular-cli, я вижу, что я построю службу вместо этого. – Brett

ответ

0

Вы не можете экспортировать функцию из NgModule,

exports : Array|any[]>

Specifies a list of directives/pipes/module that can be used within the template of any component that is part of an angular module that imports this angular module.

Подробнее об этом here

Надежда это помогает!!

0

Вы не можете экспортировать функции из модулей.

У вас есть два варианта:

1) либо создать utililty.ts файл и создать свои функции там, чтобы быть экспортированы.

export function utlityFunction1(param1, param2) { return param1+param2;) 

Вы можете импортировать эту функцию в свой код так же, как и любой другой объект.

import { utilityFunction } from './utility.ts' 

2) Создание UtilityService и впрыснуть, что в компонент.

Более подробно о том, что здесь https://angular.io/docs/ts/latest/guide/dependency-injection.html