Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

100
Visualizações
Importing a variable with import or defining it with const in the module has a behavior that I do not understand

I have been playing with this issue for a while, I am placing some styling variables (blue, red, yellow) in a separate file and I am importing them in a module to use them.

I have a problem understanding how we store the values there.

1) import statement
If I use the import statement, I can get the values in my class with a console.log(blue), console.log(eval(blue)) but I can't read it with console.log(eval(formColor)). formColor is a string that is equal to "blue", "red", or "yellow".

I get this error for console.log(eval(formColor)) and can't explain why: enter image description here

2) using const
If I define the styling variables directly at the beginning of my file, before the class definition, I can access them with a console.log(blue/eval(blue)/eval(formColor))

My question is, I want to reuse these variables somewhere else so they should be store in another file to make the code clear. How can I achieve that?

FYI, I am using Rails and Stimulus on this project.

radio_style.js (file were I define some styling variables)

const blue = { classToApplyNotChecked: [
"flex",
"justify-center",
"items-center",
"bg-white",
"rounded",
"border-2",
"border-blue-200"], classToApplyChecked: [
"flex",
"justify-center",
"items-center",
"bg-blue-200",
"rounded",
"border-2",
"border-blue-400"],};

form_controller.js (file were I want to import the styling variables in my controller class)

import { Controller } from "stimulus";
// import { blue, red, yellow } from "../plugins_variables/radio_style";

const blue = {...};

export default class extends Controller {
  static get targets() {
    return ["formWrapper", "errorMessage"];
  }
  ...
  styleRadio(){
    ...
    console.log(blue);
    console.log(eval(blue));
    console.log(formColor); // formColor retrieves a string value equal to "blue", "red" or "yellow"
    console.log(eval(formColor));
    ...
  }
}

Directories:
enter image description here

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda