导致在组件中无法使用 Angular 自定义管道的失败原因是什么?

huangapple go评论61阅读模式
英文:

What causes the failure to use an Angular custom pipe in a component?

问题

I have been working on an e-commerce app in Angular 14.

我一直在开发一个基于 Angular 14 的电子商务应用。

I have a list of products that I want to filter by brand and, for this purpose, I have made the following custom pipe (app/pipes/filter-products.pipe.ts):

我有一个产品列表,我想要按品牌进行筛选,为此,我创建了以下自定义管道(app/pipes/filter-products.pipe.ts):

import { Pipe, PipeTransform } from '@angular/core';
import { Product } from '../models/product';

@Pipe({
  name: 'filterProductsByBrand',
})
export class FilterProductsPipe implements PipeTransform {
  transform(input: Product[], brand?: String): any {
    let output: Product[] = [];
    if (brand == '') output = input;
    else output = input.filter((item) => item.brand == brand);
    return output;
  }
}

I have imported the pipe in app.modules.ts and added it to the declarations array.

我已经在 app.modules.ts 中导入了这个管道,并将它添加到了 declarations 数组中。

In the products-list.component.ts file I have:

products-list.component.ts 文件中,我有以下内容:

import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { Product, ProductResponse } from '../../models/product';
import { ProductService } from '../../services/product.service';

@Component({
  selector: 'app-products-list',
  templateUrl: './products-list.component.html',
  styleUrls: ['./products-list.component.css'],
})
export class ProductsListComponent implements OnInit {
  selectedBrand: String = '';
  productResponse!: ProductResponse;
  products: Product[];

  constructor(private ProductService: ProductService, private Router: Router) {}

  ngOnInit(): void {
    this.getProducts();
  }

  public getProducts() {
    this.ProductService.getProducts().subscribe((response) => {
      this.productResponse = response;
      this.products = this.productResponse.products;
    });
  }
}

In the above component's template:

在上面组件的模板中:

<div class="mb-3">
  <select
    [(ngModel)]="selectedBrand"
    (change)="getProducts()"
    class="form-select"
  >
    <option selected value="">All brands</option>
    <option value="Apple">Apple</option>
    <option value="Samsung">Samsung</option>
  </select>
</div>

<!-- Card list Begin -->
<ng-container *ngIf="productResponse">
  <div class="row">
    <div
      *ngFor="let product of products | filterProductsByBrand: selectedBrand"
      class="product col-sm-6 col-md-4"
    >
      <app-product-item [product]="product"></app-product-item>
    </div>
  </div>
</ng-container>
<!-- Card list End -->

The problem

问题是:

The code fails to compile and I get the error:

代码无法编译,我得到以下错误:

Property 'filterProductsByBrand' does not exist on type 'ProductsListComponent'.

在 'ProductsListComponent' 类型上不存在属性 'filterProductsByBrand'。

Stackblitz

Stackblitz示例链接: https://stackblitz.com/edit/angular-ivy-r1yymn?file=src/app/components/products-list/products-list.component.ts

Questions

问题:

  1. What am I doing wrong?

    1. 我做错了什么?
  2. What is the most reliable way to fix this issue?

    1. 修复这个问题的最可靠方法是什么?
英文:

I have been working on an e-commerce app in Angular 14.

I have a list of products that I want to filter by brand and, for this purpose, I have mede the following custom pipe (app/pipes/filter-products.pipe.ts):

import { Pipe, PipeTransform } from &#39;@angular/core&#39;;
import { Product } from &#39;../models/product&#39;;

@Pipe({
  name: &#39;filterProductsByBrand&#39;,
})
export class FilterProductsPipe implements PipeTransform {
  transform(input: Product[], brand?: String): any {
	let output: Product[] = [];
	if (brand == &#39;&#39;) output = input;
	else output = input.filter((item) =&gt; item.brand == brand);
	return output;
  }
}

I have imported the pipe in app.modules.ts and added it to the declarations arary.

In the products-list.component.ts file I have:

import { Component, OnInit } from &#39;@angular/core&#39;;
import { Router } from &#39;@angular/router&#39;;
import { Product, ProductResponse } from &#39;../../models/product&#39;;
import { ProductService } from &#39;../../services/product.service&#39;;

@Component({
  selector: &#39;app-products-list&#39;,
  templateUrl: &#39;./products-list.component.html&#39;,
  styleUrls: [&#39;./products-list.component.css&#39;],
})
export class ProductsListComponent implements OnInit {
  selectedBrand: String = &#39;&#39;;
  productResponse!: ProductResponse;
  products: Product[];

  constructor(private ProductService: ProductService, private Router: Router) {}

  ngOnInit(): void {
	this.getPoducts();
  }

  public getPoducts() {
	this.ProductService.getProducts().subscribe((response) =&gt; {
	  this.productResponse = response;
	  this.products = this.productResponse.products;
	});
  }
}

In the above component's template:

&lt;div class=&quot;mb-3&quot;&gt;
  &lt;select
	[(ngModel)]=&quot;selectedBrand&quot;
	(change)=&quot;getPoducts()&quot;
	class=&quot;form-select&quot;
  &gt;
	&gt;
	&lt;option selected value=&quot;&quot;&gt;All brands&lt;/option&gt;
	&lt;option value=&quot;Apple&quot;&gt;Apple&lt;/option&gt;
	&lt;option value=&quot;Samsung&quot;&gt;Samsung&lt;/option&gt;
  &lt;/select&gt;
&lt;/div&gt;

&lt;!-- Card list Begin --&gt;
&lt;ng-container *ngIf=&quot;productResponse&quot;&gt;
  &lt;div class=&quot;row&quot;&gt;
	&lt;div
	  *ngFor=&quot;let product of products | filter: filterProductsByBrand&quot;
	  class=&quot;product col-sm-6 col-md-4&quot;
	&gt;
	  &lt;app-product-item [product]=&quot;product&quot;&gt;&lt;/app-product-item&gt;
	&lt;/div&gt;
  &lt;/div&gt; &lt;/ng-container
&gt;&lt;!-- Card list End --&gt;

The problem

The code fails to compile and I get the error:

> Property 'filterProductsByBrand' does not exist on type 'ProductsListComponent'.

Stackblitz

I have put toghteter this stackblitz.

Questions

  1. What am I doing wrong?
  2. What is the most reliable way to fix this issue?

答案1

得分: 3

管道的选择器是`filterProductsByBrand`,但在这里,您试图使用名为`filter`的管道,并将字符串`"filterProductsByBrand"`作为第二个参数传递。

尝试使用<div *ngFor="let product of products | filterProductsByBrand: 'brand-name'">

英文:
&lt;div *ngFor=&quot;let product of products | filter: filterProductsByBrand&quot;&gt;

The selector for your pipe is filterProductsByBrand but here you are trying to use a pipe called filter and passing the string &quot;filterProductsByBrand&quot; as a second argument.

Try &lt;div *ngFor=&quot;let product of products | filterProductsByBrand: &#39;brand-name&#39;&quot;&gt;

huangapple
  • 本文由 发表于 2023年5月17日 20:51:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76272326.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定