Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React脚手架生成配置文件 #17

Open
Wscats opened this issue Dec 3, 2018 · 1 comment
Open

React脚手架生成配置文件 #17

Wscats opened this issue Dec 3, 2018 · 1 comment

Comments

@Wscats
Copy link
Owner

Wscats commented Dec 3, 2018

生成config目录

在 create-react-app 的 package.json 文件里面有一行命令

"scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
},

所以当我们执行npm run eject的时候,我们就可以在我们的项目中生成一个config的配置文件,里面可以更改 webpack 的参数

2018-12-03 3 02 23

配置sass

经过上面的操作之后,我们可以在项目中使用 sass 由于 sass 的配置环境已经给我们配置好了

// style files regexes
const cssRegex = /\.css$/;
const cssModuleRegex = /\.module\.css$/;
const sassRegex = /\.(scss|sass)$/;
const sassModuleRegex = /\.module\.(scss|sass)$/;
// code

{
      test: sassRegex,
      exclude: sassModuleRegex,
      use: getStyleLoaders({ importLoaders: 2 }, 'sass-loader'),
},

所以我们可以直接在命令行里面安装node-sass就可以在 react 项目中使用 sass 了

配置Proxy代理

由于我们在项目中经常会出现跨域,我们可以在 package.json 文件中,用 proxy 来设置服务器代理

"proxy": "http://news-at.zhihu.com",

在项目中当你就可以发请求代理到对应的连接上

React.axios.get('/api/jsons/')
    .then((response) => {
        console.log(response);
        this.setState({
            cards: response.data.data.cards
        })
    })
    .catch(function (error) {
        console.log(error);
    });

以上连接就会从/api/jsons/代理到https://news-at.zhihu.com/api/jsons/访问数据

@zq0322
Copy link

zq0322 commented Dec 6, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants