你好,我正在试图找到一种方法,在一个单独的文件中设置已定义值来填充我的函数:
我的函数index.js:
const Modes = (array) => {
return {
name: array.name,
funcionarioIncrease: array.funcionarioValor,
socioIncrease: array.socioValue,
faturamento: array.faturamento
}
}
我的schema.json
{
"name": "SIMPLES NACIONAL – MEI", "funcionarioIncrease": 49.99,
"socioIncrease": 0,
"FATURAMENTO": [
{
"name": "ATÉ 30.000,00",
"value": 149.99
},
{
"name": "De 30.001,00 a 50.000,00 ",
"value": 199.90
} ]
}
但我不确定如何实现,以及。json是否是正确的方法
您可以使用一个充满常量值的JS文件并导入它。
例如:
costants.js
export const NAME="MyTestName";
export const SOCIO_INCREASE=0;
export const FATURAMENTO=[{id:1}]
在您的功能组件中:
functional.js
import {NAME,SOCIO_INCREASE,FATURAMENTO} from "./constants.js"
const Modes = ({
name=NAME,
socioIncrease=SOCIO_INCREASE,
faturamento=FATURAMENTO
}) => {
return {
name,
socioIncrease,
faturamento
}
}
记住,如果您将其中一些道具传递给您的功能组件,默认值将被重写。
从你的问题看不清楚你到底想要什么。 基本上,你的例子是按原样工作的。 如果你理解错了,请告诉我。
// external file
const data = {
"name": "SIMPLES NACIONAL – MEI",
"funcionarioIncrease": 49.99,
"socioIncrease": 0,
"FATURAMENTO": [
{
"name": "ATÉ 30.000,00",
"value": 149.99
},
{
"name": "De 30.001,00 a 50.000,00 ",
"value": 199.90
} ]
}
// use JSON.parse() if data type is "json"
// export const array = JSON.parse(data)
export array = data // if it is an object
// main file
import { array } from "./path/toFile"
const Modes = (array) => {
return {
name: array.name,
funcionarioIncrease: array.funcionarioValor,
socioIncrease: array.socioValue,
faturamento: array.faturamento
}
}