[英]Angular2 2.0.0 How to add simple functions to @NgModule
I would like to create a "Utility Module": tnt.module.ts: 我想创建一个“实用程序模块”:tnt.module.ts:
import {NgModule} from '@angular/core';
import {toUpperCase} from "./tnt.utils";
@NgModule({
declarations: [
toUpperCase
],
exports: [
toUpperCase
]
})
export default class TntModule {}
Here is one util function example: tnt.utils.ts 这是一个util函数示例:tnt.utils.ts
export const toUpperCase= function (str:String) {
return str.toUpperCase()
}
I am getting an Error: 我收到一个错误:
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'
What am I missing ? 我错过了什么? Why can't I create a module with simple functions ?
为什么我不能创建一个功能简单的模块? I suppose I am just missing a simple detail but I can't figure it out...
我想我只是错过了一个简单的细节,但我无法弄明白......
You can not export a function from NgModule, 你无法从NgModule导出函数,
exports : Array|any[]>
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.
指定可在任何组件的模板中使用的指令/管道/模块列表,该组件是导入此角度模块的角度模块的一部分。
Read more about it here 在这里阅读更多相关信息
Hope this helps!! 希望这可以帮助!!
You can not export functions from modules. 您无法从模块导出函数。
You have two options: 您有两种选择:
1) either create a utililty.ts
file and create your functions in there to be exported. 1)创建一个
utililty.ts
文件并在那里创建要导出的函数。
export function utlityFunction1(param1, param2) { return param1+param2; )
you can import this function in your code file just like any other object. 您可以像在任何其他对象中一样在代码文件中导入此函数。
import { utilityFunction } from './utility.ts'
2) Create a UtilityService
and inject that into your component. 2)创建一个
UtilityService
并将其注入您的组件。
More on that here https://angular.io/docs/ts/latest/guide/dependency-injection.html 更多相关信息,请访问https://angular.io/docs/ts/latest/guide/dependency-injection.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.