【问题标题】:app.module.ts can't find name of pipeapp.module.ts 找不到管道的名称
【发布时间】:2017-02-24 05:52:49
【问题描述】:

我正在尝试使用 Angular 2 创建一个简单的应用程序,但在使用 Pipe 装饰器时遇到了问题。我正在使用 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 的名称,但这并没有解决问题。总的来说,我对 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 { }

管道.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',
  }
}

【问题讨论】:

    标签: angularjs angular pipe


    【解决方案1】:

    你需要将导入添加到module.ts

    import {KeysPipe} from './pipe'
    

    【讨论】:

    • 谢谢!在我将import {Component, Pipe, PipeTransform} from '@angular/core'; 添加到pipe.ts 后出现另一个错误:Pipe is not defined'。你知道这是什么原因吗?
    • 从'@angular/core'导入{管道};它应该可以工作,尝试将您的管道 ts 更改为其他名称
    猜你喜欢
    • 2020-09-21
    • 2023-01-23
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多