【发布时间】:2018-09-02 23:09:43
【问题描述】:
在我尝试实现搜索和排序管道功能以在我的 ionic 3 项目中准备搜索功能以搜索来自数据库的数据时, 我收到一个错误,我不知道为什么.. 像这样的错误:
core.js:1350 ERROR Error: Uncaught (in promise): Error: Template parses errors:
The pipe 'sort' could not be found ("
<div [ngSwitch]="pet">
<ion-list *ngSwitchCase="'users'">
<ion-item *ngFor="l[ERROR ->]et f of users | search : terms | sort: {property: column, order: order}">
<ion-thumbnail it"): ng:///SearchPageModule/SearchPage.html@32:27
The pipe 'search' could not be found ("
<div [ngSwitch]="pet">
<ion-list *ngSwitchCase="'users'">
<ion-item *ngFor="l[ERROR ->]et f of users | search : terms | sort: {property: column, order: order}">
<ion-thumbnail it"): ng:///SearchPageModule/SearchPage.html@32:27
The pipe 'sort' could not be found ("
<ion-list *ngSwitchCase="'places'">
<ion-item *ngFor="l[ERROR ->]et p of places | search : terms | sort: {property: column, order: order}">
<ion-thumbnail i"): ng:///SearchPageModule/SearchPage.html@62:27
The pipe 'search' could not be found ("
<ion-list *ngSwitchCase="'places'">
<ion-item *ngFor="l[ERROR ->]et p of places | search : terms | sort: {property: column, order: order}">
<ion-thumbnail i"): ng:///SearchPageModule/SearchPage.html@62:27
Error: Template parse errors:
The pipe 'sort' could not be found ("
<div [ngSwitch]="pet">
<ion-list *ngSwitchCase="'users'">
<ion-item *ngFor="l[ERROR ->]et f of users | search : terms | sort: {property: column, order: order}">
<ion-thumbnail it"): ng:///SearchPageModule/SearchPage.html@32:27
The pipe 'search' could not be found ("
<div [ngSwitch]="pet">
<ion-list *ngSwitchCase="'users'">
<ion-item *ngFor="l[ERROR ->]et f of users | search : terms | sort: {property: column, order: order}">
<ion-thumbnail it"): ng:///SearchPageModule/SearchPage.html@32:27
The pipe 'sort' could not be found ("
<ion-list *ngSwitchCase="'places'">
<ion-item *ngFor="l[ERROR ->]et p of places | search : terms | sort: {property: column, order: order}">
<ion-thumbnail i"): ng:///SearchPageModule/SearchPage.html@62:27
The pipe 'search' could not be found ("
<ion-list *ngSwitchCase="'places'">
<ion-item *ngFor="l[ERROR ->]et p of places | search : terms | sort: {property: column, order: order}">
<ion-thumbnail i"): ng:///SearchPageModule/SearchPage.html@62:27
at syntaxError (compiler.js:466)
at TemplateParser.parse (compiler.js:24329)
at JitCompiler._parseTemplate (compiler.js:33757)
at JitCompiler._compileTemplate (compiler.js:33732)
at compiler.js:33634
at Set.forEach (<anonymous>)
at JitCompiler._compileComponents (compiler.js:33634)
at compiler.js:33504
at Object.then (compiler.js:455)
at JitCompiler._compileModuleAndComponents (compiler.js:33503)
at syntaxError (compiler.js:466)
at TemplateParser.parse (compiler.js:24329)
at JitCompiler._parseTemplate (compiler.js:33757)
at JitCompiler._compileTemplate (compiler.js:33732)
at compiler.js:33634
at Set.forEach (<anonymous>)
at JitCompiler._compileComponents (compiler.js:33634)
at compiler.js:33504
at Object.then (compiler.js:455)
at JitCompiler._compileModuleAndComponents (compiler.js:33503)
at c (polyfills.js:3)
at c (polyfills.js:3)
at polyfills.js:3
at t.invokeTask (polyfills.js:3)
at Object.onInvokeTask (core.js:4620)
at t.invokeTask (polyfills.js:3)
at r.runTask (polyfills.js:3)
at o (polyfills.js:3)
at <anonymous>
page.html中的代码是:
<ion-searchbar [(ngModel)]="terms"></ion-searchbar>
<button ion-button type="button" (click)="sort()">Sort</button>
<div [ngSwitch]="pet">
<ion-list *ngSwitchCase="'users'">
<ion-item *ngFor="let f of users | search : terms | sort: {property: column, order: order}">
<ion-thumbnail item-start>
<img [src]="f.image"/>
</ion-thumbnail>
<h2>{{f.name}}</h2>
</ion-item>
</ion-list>
page.ts中的代码是:
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { HttpClient } from '@angular/common/http';
import { SearchPipe } from '../../pipes/search/search';
import { SortPipe } from '../../pipes/sort/sort';
@IonicPage()
@Component({
selector: 'page-search',
templateUrl: 'search.html',
})
export class SearchPage {
descending: boolean = false;
order: number;
column: string = 'name';
users : any ;
places : any ;
constructor(public navCtrl: NavController, public navParams: NavParams , public http: HttpClient) {
//this.initializeItems();
}
ngOnInit(){
this.http.get("http://localhost/Test1/getCity.php?action=getFriends")
.subscribe( data =>{
this.users = data['friends']
console.log(this.users)
});
this.http.get("http://localhost/Test1/getCity.php?action=getDetail")
.subscribe( data =>{
this.places = data['detail']
console.log(this.places)
});
}
sort(){
this.descending = !this.descending;
this.order = this.descending ? 1 : -1;
}
}
在 app.module.ts 中有:
import { SearchPipe } from '../pipes/search/search';
import { SortPipe } from '../pipes/sort/sort';
@NgModule({
declarations: [
MyApp,
TulkarmPage,
CatDetailsPage,
ModalsPage,
DetailsPage,
SearchPipe,
SortPipe
],
我将包含 pipes.ts 编码:
第一:searc.ts管道:
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'search',
})
export class SearchPipe implements PipeTransform {
transform(items: any[], terms: string): any[] {
if(!items) return [];
if(!terms) return items;
terms = terms.toLowerCase();
return items.filter( it => {
return it.name.toLowerCase().includes(terms); // only filter country name
});
}
}
第二个:sort.ts 代码
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'sort',
})
export class SortPipe implements PipeTransform {
transform(array: Array<string>, args?: any): Array<string> {
return array.sort(function(a, b){
if(a[args.property] < b[args.property]){
return -1 * args.order;
}
else if( a[args.property] > b[args.property]){
return 1 * args.order;
}
else{
return 0;
}
});
}
}
** 请在您判断其重复的问题之前,我希望您先完整阅读该问题并给我一个明显的答案或解决此错误的方法,我知道有一个问题可能会比较它,但真的我的问题没有解决方案**
谢谢大家
【问题讨论】:
标签: html angular typescript pipe ionic3