Class Interface function definition - TypeError: Object doesn't support property or method

前端 未结 3 703
生来不讨喜
生来不讨喜 2020-12-12 07:08

I\'ve written a construct similar to the following in an Angular app (this has been greatly simplified to demonstrate the issue). What would prevent the filte

3条回答
  •  孤街浪徒
    2020-12-12 07:28

    I was able to get it working the way I had originally wanted it to with the below setup. The key was, apparently, adding a filter property to the C# object that was being used to serialize data to JSON via Web API. Not sure why this would be required, as TypeScript should be able to extend the model received from Web API with additional functionality.

    Example simplified to demonstrate the problem set

    DemoModel.cs

    // C# Class JSON is serialized from via Web API
    public class DemoModel
    {
        public string displayName { get; set; }
        public string filter
        {
            get { return displayName; }
        }
    }
    

    iproperty.interface.ts

    export interface IProperty {
        filter: string;
    }
    

    demo.model.ts

    import { IProperty } from '../interfaces/iproperty.interface';
    
    export class Demo implements IProperty {
        displayName: string;
        get filter(): string { return this.displayName; }
    }
    

    core.datasource.ts

    export class CoreDataSource {
        filterChange = new BehaviorSubject('');
        filteredData: TItem[];
        get filter(): string { return this.filterChange.value; }
        set filter(filter: string) { this.filterChange.next(filter); }
    
        constructor(private service: IService) {
            super();
            this.filteredData = service.data.value.slice();
        }
    
        connect(): Observable {
            const displayDataChanges = [
                this.service.data,
                this.filterChange
            ];
    
            return Observable.merge(...displayDataChanges).map(() => {
                this.filteredData = this.service.data.value.slice().filter((item: TItem) => {
                    const searchStr = item.filter.toLowerCase();
                    return searchStr.indexOf(this.filter.toLowerCase()) !== -1;
                });
    
                return filteredData;
            });
        }
    }
    

提交回复
热议问题