【问题标题】:Property 'select' does not exist on type '{}'类型“{}”上不存在属性“选择”
【发布时间】:2019-07-29 09:08:47
【问题描述】:

我是 Angular 的新手,正在尝试做一个电子商务项目,所以我想根据 URL 的 queryParams 中的内容过滤我的产品。

所以假设我的网址是

http://localhost:4200/?category=Fruits

所以它应该过滤所有类别为水果的产品

这是代码

html文件

<div class="row ">
  <div class="col-md-3">
    <ul class="list-group" <a *ngFor="let c of categories" class="list-group-item" routerLink="/" routerLinkActive="active" [queryParams]="{category:c.name}">
      {{c.name}}
      </a>
    </ul>
  </div>
  <div class="col">
    <div class="row">
      <ng-container *ngFor="let p of products;let i=index" class="row-eq-height">
        <div class="col">
          <mdb-card style="width:17rem;" class="c1">
            <div class="view rgba-white-slight waves-light" mdbWavesEffect>
              <mdb-card-img [src]="p?.imageurl" alt="Card image cap" class="cardimg"> </mdb-card-img>
              <a>
                <div class="mask"></div>
              </a>
            </div>
            <mdb-card-body>
              <mdb-card-title>
                <h4>{{p?.title}}</h4>
              </mdb-card-title>
              <mdb-card-text>$ {{p?.price}}
              </mdb-card-text>
              <button mdbBtn type="button" color="primary" size="sm" mdbWavesEffect>Add to Cart</button>
            </mdb-card-body>
          </mdb-card>
        </div>

      </ng-container>
    </div>
  </div>


</div>

.component.ts

import { ActivatedRoute } from '@angular/router';
import { Subscription } from 'rxjs';
import { Component, OnInit, OnDestroy } from '@angular/core';
import { ProductsService } from '../services/products.service';
import { ItemsService } from '../services/items.service';
import { filter } from 'rxjs/operators';
@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.scss']
})

export class HomeComponent implements OnInit {
  products;
  categories;
  query;
  filteredproducts;
  subscription: Subscription;

  constructor(private prservice: ProductsService, private iservice: ItemsService, private activatedrouter: ActivatedRoute) { }


  ngOnInit() {
    this.subscription = this.iservice.getdata().subscribe(data => {
      this.products = data;
      console.log(this.products)
    });

    this.subscription = this.iservice.getitems().subscribe(categ => {
      this.categories = categ;
    });

    this.activatedrouter.queryParams.subscribe(p => {
      this.query = p['category'];
      console.log(this.query)

      this.filteredproducts = this.products.pipe(filter(p => p.select === this.query));
      console.log(this.filteredproducts)

    });
  }

  OnDestroy() {
    this.subscription.unsubscribe();
  }
}

这是我在控制台中获得的产品截图 https://ibb.co/FnKDvsk

现在我已经对“选择”应用了过滤功能来过滤产品

我面临的错误是

  1. 无法读取未定义的属性“管道”
  2. 类型“{}”上不存在属性“select”。

【问题讨论】:

  • 我没有收到这一行 "this.filteredproducts=this.query?this.products.pipe()" 为什么你使用 "this.query?"在你的代码中?
  • @SnehaPawar 没关系,这只是一个额外的步骤,现在我已经编辑了它!你可以再看看。
  • 错误 1:正如错误状态 this.products 未定义,因此调用它的任何成员都会产生该错误。
  • 我猜你在获取数据之前正在运行这条线this.filteredproducts = this.products.pipe。你可以通过初始化像products = []这样的产品变量来快速解决这个问题;或将此行放入this.subscription = this.iservice.getdata().subscribe(data
  • 您的 HTML 无效;可能不是你的错误的原因,但需要注意的事情。

标签: html angular typescript


【解决方案1】:

为什么会这样?因为this.products 是未定义的,而您将pipe 应用于未定义。

解决方案: 在开头初始化productsproducts = []

或者

ngOnInit() {
  this.subscription = this.iservice.getdata().subscribe(data => {
    this.products = data;
    console.log(this.products)

    this.activatedrouter.queryParams.subscribe(p => {
      this.query = p['category'];
      console.log(this.query)
      this.filteredproducts = Object.create(this.products);
      this.filteredproducts = this.filteredproducts.filter(p => p.select === this.query);
      console.log(this.filteredproducts)

    });
  });

  this.subscription = this.iservice.getitems().subscribe(categ => {
    this.categories = categ;
  });
}

【讨论】:

  • 对不起,我编辑了错误的答案。我意识到这一点后立即应用了回滚。
  • @Igor 实际上编辑是正确的。他不需要添加pipe,并且可以在产品数组上应用过滤方法。
  • @Maihan 还有一个问题,如果我点击任何类别,然后点击任何类别,我只会在第一次在控制台中获得输出,然后点击我在控制台中没有收到任何内容的任何类别!跨度>
  • @rupak 这是因为您在原始数组和过滤数组之间有引用。使用深拷贝Object.create()。请参阅编辑后的答案。
【解决方案2】:

错误 1:正如错误状态 this.products 未定义,因此调用其上的任何成员都会产生该错误。解决此问题的一种方法是将调用移动到回调内部,以便为 this.products 分配一个值。示例:

另一个错误是您试图将pipe/filter 应用于返回的数组。这只能应用于 observable,它是 rxjs 库的一部分。只需在返回的数组上直接使用Array.prototype.filter

ngOnInit() {
  this.subscription = this.iservice.getdata().subscribe(data => {
    this.products = data;
    console.log(this.products);
    this.activatedrouter.queryParams.subscribe(p => {
      this.query = p['category'];
      console.log(this.query)

      this.filteredproducts = this.products.filter(p => p.select === this.query);
      console.log(this.filteredproducts)
    });       
  });

  this.subscription = this.iservice.getitems().subscribe(categ => {
    this.categories = categ;
  });
}

错误 2:Property 'select' does not exist on type '{}'.

返回的产品实例不包含select 成员。或者这可能是在您的代码中的其他地方引起的,但这是共享代码中唯一包含对成员 select 的调用的地方。

【讨论】:

  • 还有一个问题,如果我点击任何类别,然后点击控制台中没有收到任何内容的任何类别,我只会在第一个控制台中获得输出!
猜你喜欢
  • 2018-09-29
  • 1970-01-01
  • 2021-01-09
  • 2019-05-13
  • 1970-01-01
  • 2021-07-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多