很遗憾,这是您必须自己完成的事情。不过,您可以使用像npm-watch这样的工具来实现您想要的功能:
安装npm-watch。
npm i --save-dev npm-watch
package.json
{
"name": "react-app",
"version": "0.1.0",
"private": false,
"devDependencies": {
"npm-watch": "^0.1.8",
"react-scripts": "0.9.5",
},
"dependencies": {
"react": "^15.4.2",
"react-dom": "^15.4.2"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject",
"watch": "npm-watch"
},
"watch": {
"build": "src/"
}
}
之后,只需使用
npm run watch
来启动npm-watch,以便在更改时重新构建您的资源。
更新:
现在,React-scripts包括一个
proxy
选项,它将请求代理到不同的主机/端口。例如,如果您的后端在
localhost
上以
/api
路由下的端口
9000
运行,则应将此行添加到您的package.json中:
"proxy": "localhost:9000/api"
。然后,您可以像在生产环境中一样发出请求。(来源:
https://create-react-app.dev/docs/proxying-api-requests-in-development)