我有一個基於用戶選擇的過濾器選項顯示數據的Angular 4組件。我想將過濾器選項保存爲類似kibana的url類型中的參數。在Angular中處理大量可選參數的最佳方法
我想知道什麼是處理所有這些可選參數的最佳方法。我的例子中有6個過濾器選項,它使代碼有點笨拙。
目前我正在訂閱route.params並用if語句搜索每一個。要創建和導航網址,我要訂閱事件發佈器,檢查自定義params類的值。這裏就像
this.route.params.subscribe((params: Params) => {
if (params["filter1"]) this.params.filter1 = params["filter1"];
if (params["filter2"]) this.params.filter2 = params["filter2"];
if (params["filter3"]) this.params.filter3 = params["filter3"];
if (params["filter4"]) this.params.filter4 = params["filter4"];
if (params["filter5"]) this.params.filter5 = params["filter5"];
if (params["filter6"]) this.params.filter6 = params["filter6"];
});
this.refreshParams.subscribe((params) => {
let url = {};
if (params.filter1) url["filter1"] = params.filter1;
if (params.filter2) url["filter2"] = params.filter2;
if (params.filter1) url["filter3"] = params.filter3;
if (params.filter1) url["filter4"] = params.filter4;
if (params.filter1) url["filter5"] = params.filter5;
if (params.filter1) url["filter6"] = params.filter6;
this.router.navigate(['/filters', url]);
});
selectFilter(filter1: Filter) {
if (filter1) {
this.params.filter1 = filter1;
} else {
this.params.filter1 = undefined;
}
this.refreshParams.next(this.params);
}