columns里的formItem是直接用的antd里的对应组件 比如默认 formItem: {} 这样写用的就是antd的Input组件formItem: { type: 'select' }就是antd的Select下拉组件,要写事件的话直接在formItem里定义就行了,之后会传给对应组件,例:
... { title: '角色名', name: 'roleName', formItem: { onKeyDown: e => console.log(e) } }, ...
当请求后端接口时,我们使用反向代理方式,在src/setupProxy.js中进行设置,更多设置看create-react-app v2和http-proxy-middleware
app.use( proxy('/api', { target: 'http://192.168.202.12:8391', changeOrigin: true, pathRewrite: { '^/api': '' } }) ); // 例 fetch('/api/user/getDetail') -> 'http://192.168.202.12:8391/user/getDetail' // 可配置多个, 注意顺序,最大范围的要放到最下面, app.use( proxy('/api/sub', { target: 'http://localhost:8080', changeOrigin: true, pathRewrite: { '^/api': '' } }) ); app.use( proxy('/api', { target: 'http://aaa:1000', changeOrigin: true, pathRewrite: { '^/api': '' } }) ); app.use( proxy('/xxx', { target: 'http://bbb:2000', changeOrigin: true, pathRewrite: { '^/xxx': '' } }) );
- 打开nginx配置文件
nginx.conf,与上面效果一样,在server下增加
location /api/v1/ { proxy_pass http://192.168.202.11/v1/; } location /api/v2/ { proxy_pass http://192.168.202.12/v2/; } location /api/ { proxy_pass http://192.168.202.13/; }
更多配置自行查找...
- 重启nginx
场景一:如果后台设置如下 (推荐)
Access-Control-Allow-Origin *; Access-Control-Allow-Credentials true // 重要
则前台可以直接发请求(可能会有一条options请求),不用改配置
场景二:如果后台设置如下
Access-Control-Allow-Origin *;
如果前台不改配置,会报错device:1 Failed to load http://localhost:8080/test: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. Origin 'http://localhost:3000' is therefore not allowed access.
需要前台需改配置config.js中的request
request: { prefix: '/api', credentials: 'omit', // credentials改为omit,fetch的默认值
在发请求应该就ok了,因为可能不是简单请求同样会有一条options
发布路径(1.2.0以后参考)
注:1.1.0之后这个配置被移到了package.json中 publicPath 改为 baseURL,以'/'开头,结尾没有'/'
build项目的时候注意在config-overrides.js中配置正确的publicPath,例如放到demo文件夹下为
config.output.publicPath = '/demo'; // 跟据实际项目设置
若发布到服务器的跟目录下为
config.output.publicPath = '/'; // 跟据实际项目设置
配置错则有可能加载不到相关资源
当使用browser history时,需要在nginx.conf下设置所有页面都指向index.html
server {
// ...
location / {
index index.html;
# 如果部署到/dva-boot-admin目录下
# try_files $uri $uri/ /dva-boot-admin/index.html;
# 如果部署到根目录
try_files $uri $uri/ /index.html;
}
// ...
}例:
$$.get('url').then(resp => { // ...more }).catch(e => console.err(e)); // 最好处理下错误
因为:在config.js中的afterResponse会再次抛出错误,需要我们自已处理