gpt4 book ai didi

html - 如何解决在 ionic 3 中为搜索栏实现管道时的错误(管道搜索找不到或未找到)

转载 作者:搜寻专家 更新时间:2023-10-30 21:48:58 28 4
gpt4 key购买 nike

我尝试在我的 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 编码:

首先:search.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;
}
});
}
}

** 拜托,在你判断这个问题是否重复之前,我希望你首先完整地阅读这个问题并给我一个明显的答案或这个错误的解决方案,我知道有一个问题可能比它但真的我的问题没有解决方案**

感谢大家

最佳答案

如果您想在声明它们的模块之外的任何其他模块中使用任何组件/管道,您应该导出它们。

现在,在您的情况下,您不想只从 AppModule 导出它们,因为从 AppModule

导出不是一个好主意

改为创建一个名为 PipesModule 的模块

import { SearchPipe } from '../pipes/search/search';
import { SortPipe } from '../pipes/sort/sort';
import { CommonModule } from '@angular/common';

@NgModule({
imports: [CommonModule],
declarations: [SearchPipe, SortPipe],
exports: [SearchPipe, SortPipe]
})
export class PipesModule

在任何你想使用管道的模块中导入这个模块,

app.module.ts

import { PipesModule } from '../pipes/pipes.module';

@NgModule({
imports: [.., PipesModule],
declarations: [
MyApp,
TulkarmPage,
CatDetailsPage,
ModalsPage,
DetailsPage,
],
}

关于html - 如何解决在 ionic 3 中为搜索栏实现管道时的错误(管道搜索找不到或未找到),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49463799/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com