简体   繁体   English

Angular 2-如何添加自定义类?

[英]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 谢谢

Remove Tools from declarations . declarations删除Tools There is no reason to add it if it's not a component, directive, or pipe. 如果它不是组件,指令或管道,则没有理由添加它。

There is no need to add Tools into App Module, 无需在工具模块中添加工具,

declarations: is used to declare components, directives, pipes that belongs to the current module. declarations:用于声明属于当前模块的组件,指令,管道。 Everything inside declarations knows each other. 声明中的所有内容都是彼此了解的。

so just remove Tools from declaration. 因此,只需从声明中删除Tools

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM