本文介绍了在打字稿中使用初始化程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我上课了



export class 选项{

// 主要属性

valueField:any ;
labelField:any;
sortField: string [];
searchFields: string [];
项目:任何[];
currentResults:any [];
分隔符: string ;
模式:SelectizeMode;选择
:任意;
placholder: string ;

get hasOptions(): Boolean {
返回 .items? .items.length > 0 false ;
}


构造函数(_valueField?: string ,_ labelField?: string
_searchFields?: string [],_ item?:any [],_ currentResults?:any [],
_selecetd?:any,_delimiter?: string ,_sortField?: string [],_ mode?:SelectizeMode, _placholder?: string ){


this .valueField = _valueField;
this .labelField = _labelField;
this .sortField = _sortField || [ .valueField];
this .searchFields = _searchFields || [ this .labelField, this .valueField];
.items = _items;
.delimiter = _delimiter || ' ,';
.mode = _mode || SelectizeMode.single;
this .selected = _selecetd;
this .currentResults = _currentResults || [];
.placholder = _placholder || ' select ...';
}
}





i希望在不必传递所有可选参数的情况下设置该类的对象



喜欢c#



例如我想要的那样



选项x =选项(){
分隔符:' ,'
}





我尝试了什么:



i使所有构造函数参数成为可选的



创建接口而不是它工作的类但产生另一个我需要新实例的问题所以我不得不把它变成类,因为不能接受接口的实例

解决方案


hi i have class

export class Options {

    //main properties

    valueField: any;
    labelField:any;
    sortField: string[];
    searchFields: string[];
    items: any[];
    currentResults: any[];
    delimiter: string;
    mode: SelectizeMode;
    selected: any;
    placholder: string;

    get hasOptions(): Boolean {
        return this.items ?  this.items.length > 0 : false;
    }


    constructor(_valueField?: string, _labelField?: string,
        _searchFields?: string[], _items?: any[], _currentResults?: any[],
        _selecetd?: any, _delimiter?: string, _sortField?: string[], _mode?: SelectizeMode, _placholder?: string) {


        this.valueField = _valueField;
        this.labelField = _labelField;
        this.sortField = _sortField || [this.valueField];
        this.searchFields = _searchFields || [this.labelField, this.valueField];
        this.items = _items;
        this.delimiter = _delimiter || ',';
        this.mode = _mode || SelectizeMode.single;
        this.selected = _selecetd;
        this.currentResults = _currentResults || [];
        this.placholder = _placholder || 'select...';
    }
}



i want to instate object of that class without having to pass all optional paramters

like c#

for example i want to like that

Options x =     Options(){
delimiter:','
}



What I have tried:

i made all constructor parameters as optional

create Interface instead of class it worked but produce another problem that i need new Instances so i had to make it class as can not take instance of interface

解决方案


这篇关于在打字稿中使用初始化程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 05:37