Phil
Phil

Reputation: 758

Angular 2 - how to add a custom class?

I try to add a regular class to my project but I get

zone.js:917 Uncaught Error: Unexpected value 'Tools' declared by the module 'AppModule'. 
Please add a @Pipe/@Directive/@Component annotation.

it's just a regular typescript class that I want to be able to use verywhere in my project

it is not a directive, nor pipe, nor a component

export class Tools
{
    pascalCase(text:string)
    {
        return text.charAt(0).toUpperCase() + text.slice(1);
    }
}

here is the app module

import { NgModule, ApplicationRef } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HttpModule } from '@angular/http';
import { FormsModule } from '@angular/forms';
import { LocationStrategy, HashLocationStrategy } from '@angular/common';
import { CookieService } from 'angular2-cookie/core';

...    

import { Tools } from './tools.class';

@NgModule({
    imports: [
        BrowserModule,
        HttpModule,
        FormsModule,
        routing,
        CustomErrorHandlerModule
    ],
    declarations: [
        AppComponent,
        ...
        Tools
    ],

thanks

Upvotes: 3

Views: 4865

Answers (2)

Mohit Jain
Mohit Jain

Reputation: 351

There is no need to add Tools into App Module,

declarations: is used to declare components, directives, pipes that belongs to the current module. Everything inside declarations knows each other.

so just remove Tools from declaration.

Upvotes: 1

Günter Zöchbauer
Günter Zöchbauer

Reputation: 658263

Remove Tools from declarations. There is no reason to add it if it's not a component, directive, or pipe.

Upvotes: 2

Related Questions