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 ...
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
Ich benutze den angular-cli, ich sehe danke, ich werde stattdessen einen Dienst bauen. – Brett