最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

React不提取env变量

IT培训 admin 6浏览 0评论

React不提取env变量

试图利用反应中的env变量,并获得一个

未定义

我没有使用webpack,只是dotenv。不知道为什么它不起作用。

我引用了这个

React env variables with .env

并且它没有我正在寻找的解决方案。

的package.json

{
  "name": "client",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@material-ui/core": "^3.9.1",
    "@material-ui/icons": "^3.0.2",
    "axios": "^0.18.0",
    "history": "^4.7.2",
    "http-proxy-middleware": "^0.19.1",
    "material-ui-icons": "^1.0.0-beta.36",
    "moment": "^2.24.0",
    "react": "^16.7.0",
    "react-dom": "^16.7.0",
    "react-redux": "^6.0.0",
    "react-router-dom": "^4.3.1",
    "react-scripts": "2.1.3",
    "redux": "^4.0.1",
    "redux-thunk": "^2.3.0"
  },
  "scripts": {
    "start": "PORT=8001 react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": [
    ">0.2%",
    "not dead",
    "not ie <= 11",
    "not op_mini all"
  ],
  "devDependencies": {
    "dotenv": "^6.2.0"
  }
}

Home.js

import React, {Component} from 'react';
// import axios from 'axios';
import Grid from '@material-ui/core/Grid';
import Paper from '@material-ui/core/Paper';
import {withStyles} from '@material-ui/core/styles';
import Chip from '@material-ui/core/Chip';
import {signWithGithub} from '../actions/';
import {connect} from 'react-redux';
import {compose} from 'redux';
import Avatar from '@material-ui/core/Avatar';
import Axios from '../Axios'
import {Redirect, withRouter} from 'react-router-dom';
const styles = theme => ({
    root: {
        flexGrow: 1,
        padding: 20
    },
    paper: {
        padding: theme.spacing.unit * 2,
        textAlign: 'center',
        color: theme.palette.text.secondary
    },

    chip: {
        margin: theme.spacing.unit
    }
});

class Home extends Component {

    constructor(props) {
        super(props);

        this.state = {
            user: ""
        }

    }

    componentWillMount = () => {
        console.log(process.env.BASE_GITHUB_SIGNIN);
    }

.ENV

BASE_URL=http://localhost:8000
BASE_SIGN_IN=http://localhost:8000/api/users/loginUser
BASE_GITHUB_SIGNIN=http://127.0.0.1:8000/api/users/auth/github

回答如下:

如果您使用create-react-app创建了应用程序,则env具有here等标准格式。

只需将REACT_APP添加到每个变量,这样就可以了

REACT_APP_YOUR_VARIABLE_NAME

React不提取env变量

试图利用反应中的env变量,并获得一个

未定义

我没有使用webpack,只是dotenv。不知道为什么它不起作用。

我引用了这个

React env variables with .env

并且它没有我正在寻找的解决方案。

的package.json

{
  "name": "client",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@material-ui/core": "^3.9.1",
    "@material-ui/icons": "^3.0.2",
    "axios": "^0.18.0",
    "history": "^4.7.2",
    "http-proxy-middleware": "^0.19.1",
    "material-ui-icons": "^1.0.0-beta.36",
    "moment": "^2.24.0",
    "react": "^16.7.0",
    "react-dom": "^16.7.0",
    "react-redux": "^6.0.0",
    "react-router-dom": "^4.3.1",
    "react-scripts": "2.1.3",
    "redux": "^4.0.1",
    "redux-thunk": "^2.3.0"
  },
  "scripts": {
    "start": "PORT=8001 react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": [
    ">0.2%",
    "not dead",
    "not ie <= 11",
    "not op_mini all"
  ],
  "devDependencies": {
    "dotenv": "^6.2.0"
  }
}

Home.js

import React, {Component} from 'react';
// import axios from 'axios';
import Grid from '@material-ui/core/Grid';
import Paper from '@material-ui/core/Paper';
import {withStyles} from '@material-ui/core/styles';
import Chip from '@material-ui/core/Chip';
import {signWithGithub} from '../actions/';
import {connect} from 'react-redux';
import {compose} from 'redux';
import Avatar from '@material-ui/core/Avatar';
import Axios from '../Axios'
import {Redirect, withRouter} from 'react-router-dom';
const styles = theme => ({
    root: {
        flexGrow: 1,
        padding: 20
    },
    paper: {
        padding: theme.spacing.unit * 2,
        textAlign: 'center',
        color: theme.palette.text.secondary
    },

    chip: {
        margin: theme.spacing.unit
    }
});

class Home extends Component {

    constructor(props) {
        super(props);

        this.state = {
            user: ""
        }

    }

    componentWillMount = () => {
        console.log(process.env.BASE_GITHUB_SIGNIN);
    }

.ENV

BASE_URL=http://localhost:8000
BASE_SIGN_IN=http://localhost:8000/api/users/loginUser
BASE_GITHUB_SIGNIN=http://127.0.0.1:8000/api/users/auth/github

回答如下:

如果您使用create-react-app创建了应用程序,则env具有here等标准格式。

只需将REACT_APP添加到每个变量,这样就可以了

REACT_APP_YOUR_VARIABLE_NAME

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论