react: typescript system params method optimize
Posted nyan-workflow-fc
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了react: typescript system params method optimize相关的知识,希望对你有一定的参考价值。
import * as _ from "lodash"; import paramCache from "../common/param-cache" import RequestPromise from "./axios-service/RequestPromise"; export const fetchSystemParams = () => return RequestPromise( url: ‘/api/system-parameters‘ ) const parameters = paramCache.getItem("system-params") export const getParam = async (types: string[]) => if (parameters) return mergeSpecailParams(parameters, types) else return fetchSystemParams().then(( data ) => paramCache.setItem("system-params", data) return mergeSpecailParams(data, types) ) export const getParamKeys = async (type: string) => if (parameters) return setParamKeys(parameters) else return fetchSystemParams().then(( data ) => paramCache.setItem("system-params", data) return setParamKeys(data[type]) ) const setParamKeys = (params: any) => const paramKeys: string[] = []; for (const key in params) if (params.hasOwnProperty(key)) paramKeys.push(key) return paramKeys export const getParamOptions = async (types: string[]) => if (parameters) return setParamOptions(mergeSpecailParams(parameters, types)) else return fetchSystemParams().then(( data ) => paramCache.setItem("system-params", data) return setParamOptions(mergeSpecailParams(data, types)) ) const setParamOptions = (params: any) => const paramOptions: any[] = []; _.forIn(params, (value, key) => paramOptions.push(value: key, text: value) ) return paramOptions; const mergeSpecailParams = (parameters: any, types: string[]) => let targetParam = ; _.forIn(parameters, (value, key) => if (types.includes(key)) targetParam = ...targetParam, ...value ) return targetParam
以上是关于react: typescript system params method optimize的主要内容,如果未能解决你的问题,请参考以下文章
深入浅出TypeScript- 在React项目中使用TypeScript
React + Typescript:TypeScript 错误:类型“字符串”与类型“CSSProperties”没有共同的属性
将 React-Apollo 教程改编为 Typescript 时出现 Typescript 错误