【问题标题】:fetching Data to ng-bootstrap table in angular 11以角度 11 将数据提取到 ng-bootstrap 表
【发布时间】:2021-03-25 19:56:06
【问题描述】:

大家好 我在将数据提取到数组时遇到问题,但是当我将数据放入数组时,编辑器说未定义数组

错误信息: 编译失败。

src/app/customers/customers-list/customers-list.component.ts:111:14 - 错误 TS2551:“CustomersListComponent”类型上不存在“CUSTOMERS”属性。您指的是“customers$”吗?

111 this.CUSTOMERS = 帖子; ~~~~~~~~~

src/app/customers/customers-list/customers-list.component.ts:64:3 64 个客户$: Observable; ~~~~~~~~~~ 'customers$' 在这里声明。

这是代码

import {
  Component,
  OnInit,
  PipeTransform, // table
} from '@angular/core';

import { DecimalPipe } from '@angular/common'; // table
import { FormControl } from '@angular/forms'; // table

import { Observable } from 'rxjs'; // table
import { map, startWith } from 'rxjs/operators'; // table
import {NgbModal} from '@ng-bootstrap/ng-bootstrap'; // modal
import {AddCustomerComponent} from '../add-customer/add-customer.component'; // modal


import { faFolderPlus, faPencilAlt, faTrashAlt } from '@fortawesome/free-solid-svg-icons'; // fontawsome icons
import {HttpClient} from '@angular/common/http';

// table
interface Customer {
  id: number;
  name: string;
  company: string;
  remaining: number;
  email: string;
  mobile: number;
  whats_up: number;

}

let CUSTOMERS: Customer[] = [
  {
    id: 12,
    name: 'jack',
    company: 'SDTE',
    remaining: 580,
    email: 'test@test.com',
    mobile: +456456456456,
    whats_up: +456456456
  }
];

function search(text: string, pipe: PipeTransform): Customer[] {
  return CUSTOMERS.filter(customer => {
    const term = text.toLowerCase();
    return customer.name.toLowerCase().includes(term)
      || customer.company.toLowerCase().includes(term)
      || pipe.transform(customer.remaining).includes(term)
      || customer.email.toLowerCase().includes(term)
      || pipe.transform(customer.mobile).includes(term)
      || pipe.transform(customer.whats_up).includes(term);
  });
}

@Component({
  selector: 'app-customers-list',
  templateUrl: './customers-list.component.html',
  styleUrls: ['./customers-list.component.css'],
  providers: [DecimalPipe] // table
})
export class CustomersListComponent implements OnInit {

  // table
  customers$: Observable<Customer[]>;
  filter = new FormControl('');

  faFolderPlus = faFolderPlus;
  faPencilAlt = faPencilAlt;
  faTrashAlt = faTrashAlt;



  constructor(
    pipe: DecimalPipe, // table
    private modalService: NgbModal, // modal
    private http: HttpClient // Get All Data
) {
    // table
    this.customers$ = this.filter.valueChanges.pipe(
      startWith(''),
      map(text => search(text, pipe))
    );
  }

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

  // modal
  openPopupModal() {
    const modalRef = this.modalService.open(AddCustomerComponent,{ centered: true, size: 'lg' });
    modalRef.componentInstance.name = 'World';
  }

  private getAllData() {
  this.http
    .get('http://localhost:3000/customers')
    .subscribe(
      posts => {
        console.log('GET all Data works');
        this.CUSTOMERS = posts;    // <<<<< Here is the problem ************ How can I to Fix it.

      });
  }
}

【问题讨论】:

    标签: javascript angular typescript observable


    【解决方案1】:

    this.CUSTOMERS = posts; 这指的是当前类CustomersListComponent 但你的变量在类之外所以你需要直接分配CUSTOMERS = posts; :)

    【讨论】:

    • 我尝试了这个,但不工作```错误 TS2696:“对象”类型可分配给极少数其他类型。您的意思是我们使用“任何”类型吗? “Object”类型缺少“Customer[]”类型的以下属性:length、pop、push、concat 等 26 个。 111 客户 = 帖子; ~~~~~~~~~ ```
    • 如果您建议任何方法将所有表和数据都放在 calss 中以组织代码,请告诉我
    • [ { "customer_id": "1", "name": "qq", "branch": null, "company": null, "mobile": null, "whats_up": "141 ", "email": null, "vat_id": null, "motawin": true, "created_at": "2021-12-10T21:00:00.000Z" } ] 这是我从服务器对象得到的 我该如何改变它到数组
    【解决方案2】:

    您需要指定返回类型。

    你可以试试这个:

     private getAllData() {
      this.http
        .get<Customer[]>('http://localhost:3000/customers') // <<<<< Try using this. 
        .subscribe(
          posts => {
            console.log('GET all Data works');
            CUSTOMERS = posts;
          });
      }
    

    【讨论】:

    • 它不起作用。我搜索了很多问题如何将来自服务器的对象数据转换为数组?
    • [ { "customer_id": "1", "name": "qq", "branch": null, "company": null, "mobile": null, "whats_up": "141 ", "email": null, "vat_id": null, "motawin": true, "created_at": "2021-12-10T21:00:00.000Z" } ] 这是我从服务器对象得到的我如何改变它到数组
    • 您现在有哪个错误消息?您评论的部分是一个数组,因此您无需将其更改为数组。我复制了它。对我来说,它正在工作。如果要将其添加到数组中,可以使用 CUSTOMERS.push(posts);而不是 CUSTOMERS = 帖子;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-04
    • 1970-01-01
    • 2018-07-09
    • 1970-01-01
    • 1970-01-01
    • 2019-05-13
    相关资源
    最近更新 更多