각도 재료 매트 정렬 기능에 대한 코드를 작성하기가 혼란 스럽습니다. 내 변수 dataSource를 선언하면 다음과 같은 오류가 발생합니다.MatSort 구현 - 할당 할 수없는 인수
'관찰 가능'유형의 인수를 'any []'유형의 매개 변수에 지정할 수 없습니다. 속성 길이가 '관찰 가능'유형에 없습니다.
내가 시도 할 수있는 아이디어가 있습니까? 당신이
component.ts
import { Component, OnInit, ViewEncapsulation, ViewChild } from '@angular/core';
import { Location } from '@angular/common';
import { DataSource } from '@angular/cdk/collections';
import { Observable } from 'rxjs/Observable';
import { MatTableDataSource, MatSort } from '@angular/material';
import { Report } from './../../models/report';
import { RepGenService } from './../../services/rep-gen.service';
@Component({
selector: 'app-rep-gen-report',
templateUrl: './rep-gen-report.component.html',
styleUrls: ['./rep-gen-report.component.css'],
encapsulation: ViewEncapsulation.None
})
export class RepGenReportComponent implements OnInit {
reports: Report[];
dataSource = new MyDataSource(this.repGenService);
//dataSource = new MatTableDataSource(this.repGenService.getReport()); // <--- error, not working
displayedColumns = ['report_id', 'caption'];
constructor(private repGenService: RepGenService, private location: Location) { }
ngOnInit() {
this.getRepGen_Report();
}
getRepGen_Report(): void {
this.repGenService.getReport()
.subscribe(reports => this.reports = reports);
}
save(reports: Report[]) {
this.repGenService.setReport(this.reports);
}
goBack(): void {
this.location.back();
}
//@ViewChild(MatSort) sort: MatSort;
//ngAfterViewInit() {
// this.dataSource.sort = this.sort; // therefor I need the imported MatTableDataSource
//}
}
export class MyDataSource extends DataSource<any> {
constructor(private repGenService: RepGenService) {
super();
}
connect(): Observable<Report[]> {
return this.repGenService.getReport();
}
disconnect() { }
}
service.ts
을 내 component.ts의 코드와 관련 service.ts을 발견하실 수 있습니다import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { Observable } from 'rxjs/Rx';
import 'rxjs/add/operator/map';
const httpOptions = {
headers: new HttpHeaders({ 'Content-Type': 'application/json' })
};
@Injectable()
export class RepGenService {
constructor(public http: HttpClient) { }
getReport(): Observable<any> {
return this.http.get('/api/repGen_Report')
.map(response => response);
}
setReport(reports) {
this.http.post('/api/repGen_Report/update', reports
).subscribe();
}
}
감사합니다!
편집 : 내가
또한. 뭐 했어. 당신은 DataSource 응답의'this.repGenService.getReport()'를 넘겨 주었고 Array가 아닙니다. specs : https://material.angular.io/guide/cdk-table을 참조하십시오. 그 이유는 위의 오류를받는 이유입니다 :' 'Observable'유형의 인수를 'any []'유형의 매개 변수에 지정할 수 없습니다. 'Observable'유형에 속성 길이가 없습니다. –
이 코드는 어디에 써야합니까? – dafna
내 대답을 다시 형식화합니다. –