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

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

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):

  1. import { Pipe, PipeTransform } from '@angular/core';
  2. import { Product } from '../models/product';
  3. @Pipe({
  4. name: 'filterProductsByBrand',
  5. })
  6. export class FilterProductsPipe implements PipeTransform {
  7. transform(input: Product[], brand?: String): any {
  8. let output: Product[] = [];
  9. if (brand == '') output = input;
  10. else output = input.filter((item) => item.brand == brand);
  11. return output;
  12. }
  13. }

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 文件中,我有以下内容:

  1. import { Component, OnInit } from '@angular/core';
  2. import { Router } from '@angular/router';
  3. import { Product, ProductResponse } from '../../models/product';
  4. import { ProductService } from '../../services/product.service';
  5. @Component({
  6. selector: 'app-products-list',
  7. templateUrl: './products-list.component.html',
  8. styleUrls: ['./products-list.component.css'],
  9. })
  10. export class ProductsListComponent implements OnInit {
  11. selectedBrand: String = '';
  12. productResponse!: ProductResponse;
  13. products: Product[];
  14. constructor(private ProductService: ProductService, private Router: Router) {}
  15. ngOnInit(): void {
  16. this.getProducts();
  17. }
  18. public getProducts() {
  19. this.ProductService.getProducts().subscribe((response) => {
  20. this.productResponse = response;
  21. this.products = this.productResponse.products;
  22. });
  23. }
  24. }

In the above component's template:

在上面组件的模板中:

  1. <div class="mb-3">
  2. <select
  3. [(ngModel)]="selectedBrand"
  4. (change)="getProducts()"
  5. class="form-select"
  6. >
  7. <option selected value="">All brands</option>
  8. <option value="Apple">Apple</option>
  9. <option value="Samsung">Samsung</option>
  10. </select>
  11. </div>
  12. <!-- Card list Begin -->
  13. <ng-container *ngIf="productResponse">
  14. <div class="row">
  15. <div
  16. *ngFor="let product of products | filterProductsByBrand: selectedBrand"
  17. class="product col-sm-6 col-md-4"
  18. >
  19. <app-product-item [product]="product"></app-product-item>
  20. </div>
  21. </div>
  22. </ng-container>
  23. <!-- 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):

  1. import { Pipe, PipeTransform } from &#39;@angular/core&#39;;
  2. import { Product } from &#39;../models/product&#39;;
  3. @Pipe({
  4. name: &#39;filterProductsByBrand&#39;,
  5. })
  6. export class FilterProductsPipe implements PipeTransform {
  7. transform(input: Product[], brand?: String): any {
  8. let output: Product[] = [];
  9. if (brand == &#39;&#39;) output = input;
  10. else output = input.filter((item) =&gt; item.brand == brand);
  11. return output;
  12. }
  13. }

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:

  1. import { Component, OnInit } from &#39;@angular/core&#39;;
  2. import { Router } from &#39;@angular/router&#39;;
  3. import { Product, ProductResponse } from &#39;../../models/product&#39;;
  4. import { ProductService } from &#39;../../services/product.service&#39;;
  5. @Component({
  6. selector: &#39;app-products-list&#39;,
  7. templateUrl: &#39;./products-list.component.html&#39;,
  8. styleUrls: [&#39;./products-list.component.css&#39;],
  9. })
  10. export class ProductsListComponent implements OnInit {
  11. selectedBrand: String = &#39;&#39;;
  12. productResponse!: ProductResponse;
  13. products: Product[];
  14. constructor(private ProductService: ProductService, private Router: Router) {}
  15. ngOnInit(): void {
  16. this.getPoducts();
  17. }
  18. public getPoducts() {
  19. this.ProductService.getProducts().subscribe((response) =&gt; {
  20. this.productResponse = response;
  21. this.products = this.productResponse.products;
  22. });
  23. }
  24. }

In the above component's template:

  1. &lt;div class=&quot;mb-3&quot;&gt;
  2. &lt;select
  3. [(ngModel)]=&quot;selectedBrand&quot;
  4. (change)=&quot;getPoducts()&quot;
  5. class=&quot;form-select&quot;
  6. &gt;
  7. &gt;
  8. &lt;option selected value=&quot;&quot;&gt;All brands&lt;/option&gt;
  9. &lt;option value=&quot;Apple&quot;&gt;Apple&lt;/option&gt;
  10. &lt;option value=&quot;Samsung&quot;&gt;Samsung&lt;/option&gt;
  11. &lt;/select&gt;
  12. &lt;/div&gt;
  13. &lt;!-- Card list Begin --&gt;
  14. &lt;ng-container *ngIf=&quot;productResponse&quot;&gt;
  15. &lt;div class=&quot;row&quot;&gt;
  16. &lt;div
  17. *ngFor=&quot;let product of products | filter: filterProductsByBrand&quot;
  18. class=&quot;product col-sm-6 col-md-4&quot;
  19. &gt;
  20. &lt;app-product-item [product]=&quot;product&quot;&gt;&lt;/app-product-item&gt;
  21. &lt;/div&gt;
  22. &lt;/div&gt; &lt;/ng-container
  23. &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'">

英文:
  1. &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:

确定