简体   繁体   English

angular2-datatable中的Angular 2搜索文件管理器查询-类型'{}'不存在属性'first_name'

[英]Angular 2 search filer query in angular2-datatable - Property 'first_name' does not exist on type '{}'

In my Angular-cli project I've this code implemented for datatable with search filter. 在我的Angular-cli项目中,我为带有搜索过滤器的数据表实现了此代码。 I've used tutorial given in following link using loadsh. 我已经使用loadsh在以下链接中给出了教程。 http://plnkr.co/edit/grhag1P85teN4ftggkms?p=preview http://plnkr.co/edit/grhag1P85teN4ftggkms?p=preview

Here is package link: https://github.com/mariuszfoltak/angular2-datatable 这是包链接: https : //github.com/mariuszfoltak/angular2-datatable

I want to add search filter and it showed me this error in updateData function. 我想添加搜索过滤器,它在updateData函数中向我显示了此错误。 Property 'first_name' does not exist on type '{}'. 类型“ {}”上不存在属性“ first_name”。 Is it problem with typescript or loadsh version or something else? Typescript或loadsh版本还是其他问题?

export class UsersComponent implements OnInit {

    public users: any = [];
    public filteredData: any;
    public filterQuery = "";
    public rowsOnPage = 10;
    public sortBy = "first_name";
    public sortOrder = "asc";

    ngOnInit() {
        this.authService.getUsersList(userData).subscribe(data => {
            var res = data.response;
            this.users = this.users;
            this.filteredData = res.data;
        });
    }

    public updateData(query:string) {
        if(query) {
            this.filteredData = _.filter(this.users, (a)=>a.first_name.indexOf(query)>=0);
        } else {
            this.filteredData = this.users;
        }
    }
}

Here is my package.json file with loadsh version 这是我的带有loadsh版本的package.json文件

"dependencies": {
  "angular2-datatable": "^0.6.0",
  "lodash": "^4.17.4",
},
"devDependencies": {

  "@types/lodash": "^4.14.50",
  "typescript": "~2.0.3"
}

I guess this error occured in your IDE (so it's typescript error) so it's all about that your users array has type any and typescript can't detect first_name property in type which I'm guessing lodash returns as {} due to lack of specified type of array users . 我猜这个错误发生在您的IDE中(所以是打字稿错误),这是因为您的users数组具有类型any而且打字稿无法检测到type_type属性,我猜由于没有指定,lodash返回为{}数组users类型。 It's the best practise to always create class to represent data. 最佳做法是始终创建类来表示数据。 Something like: 就像是:

export class User {
    public first_name: string;
    /* And the rest of data which coming from your db */
}

/* in your component */
public users: Array<User> = [];

Defining class will help you with keep your code clean and organise. 定义类将帮助您保持代码的清洁和井井有条。 If you just don't want to create classes just use as syntax and wrap it with ( ) as shown below. 如果您不想创建类,只需将其as语法并用( )将其包装,如下所示。

this.filteredData = _.filter(this.users, (a)=>(a as any).first_name.indexOf(query)>=0);

PS If you want to use type any and it's array use any[] or Array<any> PS如果要使用any类型,并且其数组使用any[]Array<any>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM