2016-09-25 1 views
1

Ich mag würde ein "Utility-Modul" erstellen: tnt.module.ts:Angular2 2.0.0 Wie einfach Funktionen hinzufügen, um @NgModule

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

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

Hier ist eine util-Funktion Beispiel: tnt.utils. ts

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

ich erhalte eine Fehlermeldung:

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' 

Was bin ich? Warum kann ich kein Modul mit einfachen Funktionen erstellen? Ich glaube, ich bin nur ein einfaches Detail fehlt, aber ich kann es nicht herausgefunden ...

+0

Ich kann es von der Info zur Verfügung gestellt nicht beantworten. Allerdings kann ich Ihnen raten, den eckigen cli zu verwenden: https://github.com/angular/angular-cli und toUppercase scheint mir ein "Pipe" -Kandidat zu sein. Für Funktionen sollten Sie einen Service verwenden. – user3791775

+0

Ich benutze den angular-cli, ich sehe danke, ich werde stattdessen einen Dienst bauen. – Brett

Antwort

0

Sie können keine Funktion aus NgModule exportieren,

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.

Lesen Sie mehr darüber here

Hoffnung das hilft!!

0

Sie können keine Funktionen von Modulen exportieren.

Sie haben zwei Möglichkeiten:

1) entweder eine utililty.ts-Datei erstellen und Ihre Funktionen erstellen dort exportiert werden.

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

Sie können diese Funktion wie jedes andere Objekt in Ihre Codedatei importieren.

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

2) Erstellen Sie eine UtilityService und injizieren, dass in Ihrer Komponente.

Mehr dazu hier https://angular.io/docs/ts/latest/guide/dependency-injection.html

Verwandte Themen