我试图使用Angular 2创建一个简单的应用程序,并且在使用Pipe decorator时遇到了麻烦。我使用Pipe来访问数据的关键值,我在Angular 1中可以很容易地做到这一点。为了使用PIPE实现这一点,我几乎复制了一个template张贴在相关的SO question我的app.module.ts
除外。
ERROR in /Users/me/thisapp/mybio/src/app/app.module.ts (20,5): Cannot find name 'KeysPipe'.)
我尝试在我的app.module.ts
声明中复制Pipe的名称,但这并没有解决问题。总的来说,我很遗憾组件,模块和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',
}
}
答案 0 :(得分:3)
您需要将导入添加到module.ts
import {KeysPipe} from './pipe'