0
我正在尝试使用Angular 2创建一个简单的应用程序,并且在使用Pipe装饰器时遇到问题。我正在使用Pipe来访问数据值,这在Angular 1中可以非常轻松地完成。为了使用PIPE实现这个功能,我几乎复制了template发布在相关的SO question中,但我的app.module.ts
除外。app.module.ts找不到管道名称
ERROR in /Users/me/thisapp/mybio/src/app/app.module.ts (20,5): Cannot find name 'KeysPipe'.)
我试图复制管道的名字在我的app.module.ts
声明,但这并没有解决问题。总的来说,我很迷惑Component,Module和pipe.ts文件如何交互。如果能够解释问题,我将不胜感激。
app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { Pipe, PipeTransform } from '@angular/core';
import { routes } from './app.router';
import { AppComponent } from './app.component';
import { AppResumeComponent } from './app-resume/app-resume.component';
import { EducationComponent } from './education/education.component';
import { WorkxpComponent } from './workxp/workxp.component';
import { LaughComponent } from './laugh/laugh.component';
@NgModule({
declarations: [
AppComponent,
AppResumeComponent,
EducationComponent,
WorkxpComponent,
LaughComponent,
KeysPipe
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
routes
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
pipe.ts
@Pipe({name:'keys'})
export class KeysPipe implements PipeTransform {
transform(value,args:string[]) : any {
let keys = [];
for (let key in value){
keys.push({key: key, value: value[key]});
}
return keys
}
}
laugh.component.ts
import {Component, Pipe, PipeTransform} from '@angular/core';
import {KeysPipe} from './pipe'
@Component({
selector: 'my-app',
templateUrl: 'laugh.component.html',
})
export class LaughComponent {
demo = {
'key1': 'ANGULAR 2',
'key2': 'Pardeep',
'key3': 'Jain',
}
}
谢谢!从'@ angular/core';'到'pipe.ts':'管道未定义'添加'import {Component,Pipe,PipeTransform}后出现另一个错误。你知道这是什么原因吗? – st4rgut
从'@ angular/core'导入{Pipe};它应该工作,尝试改变你的管道到另一个名字 – Sajeetharan