【问题标题】:Uncaught (in promise): Error: Template parse errors: The pipe 'keys' could not be found未捕获(承诺):错误:模板解析错误:找不到管道“键”
【发布时间】:2018-01-03 12:17:54
【问题描述】:

这是我的第一篇文章,所以如果我不遵守某些规则,我深表歉意。 这篇文章的标题可能敲响了警钟,因为我查看了它周围的所有结果,但找不到问题的根本原因。

我有一个模式,可以打开以显示一个表单,在该表单中我有一个选择,它将列出枚举中的选项。我正在对这个枚举应用一个管道以使该对象成为一个数组。

但我遇到了管道“键”未找到问题。

非常感谢您的帮助!

所以我的 app.module.ts

import { ErrorHandler, NgModule } from '@angular/core';
import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular';
import { WaitingTime, YearsAgo, SortBy, KeysPipe} from '../pipes/mypipe';
import { MyApp } from './app.component';
import { HomePage } from '../pages/home/home';

@NgModule({
  declarations: [
    MyApp,
    HomePage,
    WaitingTime,
    YearsAgo,
    SortBy,
    KeysPipe //declaring my pipe here
  ],
  imports: [
    BrowserModule,
    IonicModule.forRoot(MyApp)
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    HomePage
  ],
  providers: [
    {provide: ErrorHandler, useClass: IonicErrorHandler},
    MyDateListService    
  ]
})
export class AppModule {}

然后是我的主页 home.ts(将省去一些不必要的行)。 模态框将从这里打开。

import { Component } from '@angular/core';
import { NavController, ModalController, AlertController, ItemSliding} from 
'ionic-angular';
import {DateFormPage} from '../date-form/date-form'
import {WaitingTime, YearsAgo} from '../../pipes/mypipe';
import {MyDates } from '../../models/my-dates';
import {MyDateListService} from '../../services/date-list'

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})

export class HomePage {}

打开 date-form.ts 的模态页面 我需要管道在其中发挥作用

import { Component, OnInit } from '@angular/core';
import { IonicPage, NavController, NavParams, ViewController } from 'ionic-
angular';
import {NgForm, FormBuilder, FormControl, FormGroup, Validators} from 
'@angular/forms'
import { MyDateListService } from '../../services/date-list';
import {KeysPipe} from '../../pipes/mypipe'; //here is the pipe
import {DateTypes} from '../../models/enums';

@IonicPage()
@Component({
  selector: 'page-date-form',
  templateUrl: 'date-form.html'
})

export class DateFormPage implements OnInit {}

最后是我的管道mypipe.ts

import {Pipe, PipeTransform} from '@angular/core';

//declaring all my pipes
@Pipe ({
    name:'waitingTime'
})

export class WaitingTime implements PipeTransform
{   }
[.... all the other pipes]

// and this is the pipe that is not found.
@Pipe ({ 
    name: 'keys', 
    pure: false 
})

export class KeysPipe implements PipeTransform {

    transform(value: any, args: any[] = null): any {
        return Object.keys(value).map(key => value[key]);
    }
}

【问题讨论】:

  • WaitingTime 管道工作正常吗?还有错误来自哪个页面?
  • 我没有看到模块中声明的DateFormPage
  • @ZackSunderland 是的,之前声明的所有其他管道都已找到并且工作正常(在主页中使用。
  • @acdcjunior DateFormPage 加载并且运行良好(当没有管道时)如果您的意思是在 app.module.ts 中?已经尝试过了,它给了我这个错误:未捕获(承诺):错误:类型 DateFormPage 是 2 个模块声明的一部分:AppModule 和 DateFormPageModule!已经在 home.ts 中导入了
  • @Jojo 究竟是什么错误,它来自哪个页面?

标签: angular pipe


【解决方案1】:

好的,我终于找到了我的错误,可能是因为还是个菜鸟,没有注意到ionic自动创建的页面。

我试图在我的 date-form.ts 中获取 @NgModule,但我已经有一个定义我的模块的 date-form.module.ts。 所以在那里声明我的管道,并从另一个模块 app.module.ts 中删除它的声明使它工作。

感谢你们所有的 cmets,他们引导我了解我的问题的根源。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-24
    • 2017-02-17
    • 2020-03-21
    • 1970-01-01
    • 2017-10-05
    • 1970-01-01
    • 2022-12-19
    • 2019-04-25
    相关资源
    最近更新 更多