Laravel 9 adicionou Vite para desenvolvimento front-end React JS e Vue JS e se você precisar adicionar variável de ambiente no arquivo .env e precisar acessar a variável .env no aplicativo react js, então eu o ajudarei a obter a variável de ambiente no laravel react aplicativo js. Laravel adicionou import.meta.env para acessar todas as variáveis .env no arquivo js.
Neste tutorial, aprenderemos como obter variáveis de ambiente no Laravel React Vite Para entender como obter variáveis de ambiente no Laravel React JS, fazemos o seguinte exemplo
Adicionar variável de ambiente:
VITE_APP_NAME="Vite React Demo Title"
Variável de ambiente de acesso:
Agora, vamos acessar a variável de ambiente "VITE_APP_NAME" com o título no arquivo react js. vamos ver a solução abaixo com um exemplo:
import.meta.env.VITE_APP_NAME;
import './bootstrap';
import '../css/app.css';
import React from 'react';
import { render } from 'react-dom';
import { createInertiaApp } from '@inertiajs/inertia-react';
import { InertiaProgress } from '@inertiajs/progress';
import { resolvePageComponent } from 'laravel-vite-plugin/inertia-helpers';
const appName = import.meta.env.VITE_APP_NAME;
createInertiaApp({
title: (title) => `${title} - ${appName}`,
resolve: (name) => resolvePageComponent(`./Pages/${name}.jsx`, import.meta.glob('./Pages/**/*.jsx')),
setup({ el, App, props }) {
return render(<App {...props} />, el);
},
});
InertiaProgress.init({ color: '#4B5563' });
Codificação feliz!!!